[Tutor] OO newbie

Jacob S. keridee at jayco.net
Fri Apr 8 03:18:44 CEST 2005

> 2-
> what are the differences between
> self.__DoubleUnderscore
> self._SimpleUnderscore

Double Underscores are way cooler!
Here's why.
Single underscores are just subtle clues to the user of the class that you 
aren't specifically supposed to call that function of the class. IOW, its 
used internally but not supposed to be used externally.

Double underscores are much neater.
For example --  in the python docs it should say that

int([x]) calls the __int__ method of x
float([x])  calls the __float__ method of x

And all of the other functions similar.  str, repr, etc.
Yet even cooler---

a + b  is the same as  a.__add__(b)
a - b  is the same as a.__sub__(b)
a * b is the same as a.__mul__(b)
a / b is the same as a.__div__(b)  when from __future__ import division is 
not imported

This means that you can control how the operands affect your object!
Even cooler...
Say the first argument is not your object

a + b  is the same as  b.__radd__(a)  where b is your object

This allows you to affect how other objects treat your object.

a += b  is the same as a.__iadd__(b)

It says in the docs that you should make the __i...__ series try to modify 
the object, whereas the normal ones return a new object.

etcetera, etcetera, etcetera.

To find the full series in the docs -- 



More information about the Tutor mailing list