What's the meaning of underscores (_ & __) in Python variable names?

What's the meaning of underscores (_ & __) in Python variable names?

Real Python

7 лет назад

424,533 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Marunji Marunji
Marunji Marunji - 22.10.2023 17:17

Thank you, the video covered everything I needed in a very convenient amount of time

Ответить
Joe King
Joe King - 09.03.2023 15:00

Super informative, thks.

Ответить
HTGG
HTGG - 21.10.2022 12:15

self.__mifflin='paper company'

Ответить
Usman Ghani
Usman Ghani - 11.10.2022 02:19

So for the encapsulation, should we use single underscore or double? Thanks.

Ответить
luis M
luis M - 08.03.2022 23:19

Someone tells me how to type this long underscore

Ответить
David Carlson
David Carlson - 03.03.2022 22:01

How do you get those sweet suggestions in your shell's python interpreter?

Ответить
Justanoobcoder
Justanoobcoder - 01.01.2022 04:20

python is a cool language except its OOP concept

Ответить
ZomnY
ZomnY - 04.12.2021 14:28

Dunder Mifflin .. hue hue

Ответить
Pranav Nyavanandi
Pranav Nyavanandi - 02.12.2021 05:52

How are we able to access magic methods normally then? Are they not name mangled? That is,
"Hello".__len__()
"Hello"._str__len__()
The first one works fine despite being a private method. The second I never tried.

Ответить
Danilo Silva
Danilo Silva - 13.11.2021 00:54

__attr == _Class__attr

Ответить
Ranjan Nanda
Ranjan Nanda - 02.11.2021 08:28

Thanks for making this useful video

Ответить
Andy Anderson
Andy Anderson - 27.10.2021 23:56

Good explanation, only suggestion is to use t._dict_ instead of dir(t) since it won't return many builtin methods.

Ответить
# EASY GERMAN
# EASY GERMAN - 26.10.2021 05:05

Hi,Sir Dan.JV here again.Your video is really nice and I could understand everything,sir.I have view this video,like this video and subscribed the channel,sir.

Ответить
Jon Disnard
Jon Disnard - 29.09.2021 15:29

Please correct me if wrong, but I was under the impression the _init_ is merely an initializer, where _new_ is the underlying constructor?

Ответить
Javlon Tursunov
Javlon Tursunov - 18.09.2021 12:23

Hey bro please explain how to write double merged underscores

Ответить
Harmita Lestari
Harmita Lestari - 30.07.2021 10:22

Hi, sir.. How to remove these underscore?

Ответить
Gerard Gandionco
Gerard Gandionco - 15.06.2021 10:56

Thank you sir!

Ответить
Richard Hutchinson
Richard Hutchinson - 08.06.2021 19:29

You did not show how to access the dunder bazz __baz within the class.

Do you use the class name, then dot notation to get to __baz?

Example:
Test.__baz
>>> 42

Is this how to get the 42?

Ответить
KusogeMan
KusogeMan - 06.06.2021 19:51

single underscore usually means private variable, double (dunder) means new classes will use the name mangling correctly with variables with same name
dir() returns attributes

Ответить
Canon McLarnon
Canon McLarnon - 05.06.2021 21:49

I would've loved to see what happens when you extend your Test class. Like would there be _Test__baz AND _extendedTest__baz or just the latter? I guess I'll have to go do it myself...

Ответить
Tony Houghton
Tony Houghton - 04.06.2021 01:52

Now I want to call all my variables __mifflin.

Ответить
Gio' Leyk
Gio' Leyk - 02.06.2021 16:24

-__-

Ответить
Dmitrii Petriev
Dmitrii Petriev - 31.05.2021 13:31

What interpreter you use? It’s give some tips when you type function. I have never seen that before.

Ответить
jamcdonald120
jamcdonald120 - 31.05.2021 03:46

but what if I want a variable named _Test__baz?

Ответить
Pronto
Pronto - 30.05.2021 06:36

Yay! Subbed.

Ответить
Paul Abraham
Paul Abraham - 30.05.2021 02:24

Blimey. The more I see of python the more I see a horrible mess.

Ответить
Tesohh
Tesohh - 29.05.2021 22:17

How do you have syntax highlighting and suggestions?

Ответить
dika2saja
dika2saja - 29.05.2021 04:03

Cus it is _snake_

Ответить
Gavin Pereira
Gavin Pereira - 28.05.2021 10:08

Anybody know what console / editor this is. It looks pretty cool

Ответить
Rayan
Rayan - 26.05.2021 16:47

i still dont get it

Ответить
Hans Bezemer
Hans Bezemer - 22.05.2021 16:41

OMG. Ugly hack after ugly hack. Probably learned it from C++. You can't you do the sensible thing an just make the thing private? Inaccessible. Out of the symbol table.

Ответить
logan nasty
logan nasty - 21.05.2021 18:25

I wish I was this clear when explaining concepts

Ответить
MidnightSt
MidnightSt - 15.05.2021 04:25

I have seen stringly typing, we all have, php and js used to use it, but this is probably the first time I've ever seen stringly scoping.

Ответить
Binil George
Binil George - 10.05.2021 13:47

A third underscore will make Thunder

Ответить
somarpy
somarpy - 07.05.2021 02:46

Plz Plz add arabic caption

Ответить
jeezboi
jeezboi - 03.05.2021 18:03

This is such a nice video...clear concept, beautiful

Ответить
P H
P H - 02.05.2021 16:25

Yikes. The more I look at Python, the more I wonder why anyone would use it? 🤔

Ответить
0
0 - 24.04.2021 11:38

.

Ответить
8koi
8koi - 24.04.2021 00:26

I don't get it, gonna come back in a few weeks!

Ответить
Dougal Tolan
Dougal Tolan - 20.04.2021 20:47

And anther reason I'll never use this garbage language.

Ответить
AlexXx
AlexXx - 19.04.2021 09:57

No real access restrictions. Geat language!

Ответить
ерунда сэндвич
ерунда сэндвич - 19.04.2021 05:39

Took you 1.min 32 seconds to begin explaining the differences.

Ответить
DynoosHD
DynoosHD - 17.04.2021 10:48

So how would be the correct way to access __baz ?

Ответить
Roman Dryndik
Roman Dryndik - 17.04.2021 08:38

What shell do you use?

Ответить
JP W
JP W - 15.04.2021 21:13

python is so messy, in other languages like js you dont have to worry about this

Ответить
Kunal Soni
Kunal Soni - 15.04.2021 15:29

wow new concept you explaining 😍

Ответить
LilacDoe
LilacDoe - 13.04.2021 22:14

What IDE is this?

Ответить
Álvaro E
Álvaro E - 13.04.2021 17:16

All the things about Python you never had the courage to ask about

Ответить