StarPilgrim said:
Brand new to python. I was wondering what the __ underscore means?
For example, there is a line of code:
__name__=='__main__'
and I don't know what the double underscore is used for.
Ah, tricky. That's not just double underscore; it's double ended
double underscore. Double ended double underscore means that it
invokes special behavior in the Python language. It means fair
warning. Look this up.
Never name your own variables with double ended double underscore. The
one exception is if you are proposing a change to the Python language
and seeking the blessing of our BDFL. Such names are reserved for the
Python language.
Leading double underscore without trailing double underscore means
that the programmer knows and loves some other object-oriented
language, and this other language has a notion of trying to enforce
that this member variable is "private", and Python is meeting him half
way. The programmer of the class advises you not to manipulate this
member variable directly and Python has bowed to convention and done
some name mangling. It's often useful, usually naive, fundamentally
insecure, and tangential to the zen Python.
A lesser known Python convention is the double ended single
underscore. Whether it even rates as convention might be arguable, but
it's there in the critical _fields_ member of the Structure and Union
base classes in the standard ctypes module. It means special within
the particular class. To the Python language it's just another name,
but the authors of the class have coded it to look up that name and do
something interesting with the associated value.
-Bryan