[Tutor] under, under

Dave Angel davea at davea.name
Mon May 13 19:18:45 CEST 2013


On 05/13/2013 12:21 PM, Stafford Baines wrote:
> Please explain the significance of __some term__.  For example  __name__ as
> in
>
> If __name__ == '__main__':
>
>    main()
>
> When is the under, under used?
>

(Please don't start a second thread with identical content 20 minutes 
after the first)

Underscores aren't anything special to the Python language itself, 
whether leading or trailing.  Thus there is no implicit connection 
between __name__ and name, for example.  However there is a convention 
for single and double underscores, and when the latter are at both start 
and end of a symbol, they have the cute nickname of dunder.

Dunder names are ones defined by the language as having special purpose. 
  We should never make up our own such names, as we might conflict with 
a dunder name that gets added in a later version of Python.

There are a few of them that are just data. One example is the __name__ 
builtin, and it is defined automatically by the import mechanism.  And 
since the script itself is "sort-of" imported, it gets a special name of 
a literal "__main__"   This lets you write code that behaves differently 
when run as a script then when it's imported explicitly from another 
module or script.

Most are methods, and these method names are called "special methods." 
The __init__() method for initializing is the most important, since it's 
implicitly called when a class instance is being initialized.  Likewise 
__new__().  Another (__str__()) is called implicitly when you try to 
interpret an object as a string (such as when you print it).

The debugger uses the __repr__() special method.  When you use the 
addition syntax
     a + b

you'll be using the __add__() and/or the __radd__() methods.

All these are pre-defined for the built-in types.  And you can see such 
a list of them for a given type by doing something like:
   a = list()
   print dir(a)

In the debugger, you might get:

  dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', 
'__delslice__', '__doc__', '__eq__', '__format__', '__ge__', 
'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', 
'__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', 
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', 
'__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

A key point is you can defined these in your own classes.  So you can 
define for example what it means for instances to be equal, or how you 
"add" them, or "subscript" them.

Normally, you do not directly call most of these special methods, 
they'll be called implicitly by various other means.  But you do write 
them in your code.


See:
http://docs.python.org/2/reference/datamodel.html#special-method-names

for a start.

-- 
DaveA


More information about the Tutor mailing list