[Tutor] OO newbie

Brian van den Broek bvande at po-box.mcgill.ca
Fri Apr 8 06:00:40 CEST 2005


Jacob S. said unto the world upon 2005-04-07 21:18:
>> 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

<SNIP>

Hi Cedric, Jacob, and all,

Jacob's answer focused on special methods -- those with both leading 
*and* trailing underscores. They're pretty cool, but methods with 
leading double underscores *only* are cool in a different way. They 
approximate `private' methods from other languages, only rather than 
being fully private, they are just really `shy'.

 >>> class A:
... 	def __init__(self):
... 		print 42
... 		
 >>> a = A()
42
 >>> a.__init__()
42
 >>>

So A.__init__ can be called like any other A method (if A had other 
methods, that is).

Contrast that with this example where there is a `shy' method with one 
leading underscore and a mangled method with 2:

 >>> class Shy:
     '''My silly example'''
     def _shy_method(self):
	'''I am a shy docstring'''
         print "I'm a shy method"
     def public_method(self):
	'''I am a public docstring'''
         print "I'm a public method"
     def __mangled_method(self):
	'''I'm a very shy docstring'''
         print "I'm quite shy and a bit of a challenge to call as"
         print "I've been mangled!"


 >>> help(Shy)
Help on class Shy in module __main__:

class Shy
  |  My silly example
  |
  |  Methods defined here:
  |
  |  public_method(self)
  |      I am a public docstring


So, either 1 or 2 leading underscores hides a method from pydoc. We 
can still get to them though:


 >>> help(Shy._shy_method)
Help on method _shy_method in module __main__:

_shy_method(self) unbound __main__.Shy method
     I am a shy docstring

 >>> help(Shy.__mangled_method)

Traceback (most recent call last):
   File "<pyshell#22>", line 1, in -toplevel-
     help(Shy.__mangled_method)
AttributeError: class Shy has no attribute '__mangled_method'
 >>> help(Shy._Shy__mangled_method)
Help on method __mangled_method in module __main__:

__mangled_method(self) unbound __main__.Shy method
     I'm a very shy docstring

The double underscore in a method name leads to so called "name 
mangling". As the example shows, we have to preface the method with 
_ClassName in order to access it.

This works the same for method calls:

 >>> shy.public_method()
I'm a public method
 >>> shy._shy_method()
I'm a shy method
 >>> shy.__mangled_method()

Traceback (most recent call last):
   File "<pyshell#26>", line 1, in -toplevel-
     shy.__mangled_method()
AttributeError: Shy instance has no attribute '__mangled_method'
 >>> shy._Shy.__mangled_method()

Traceback (most recent call last):
   File "<pyshell#27>", line 1, in -toplevel-
     shy._Shy.__mangled_method()
AttributeError: Shy instance has no attribute '_Shy'
 >>> shy._Shy__mangled_method()
I'm quite shy and a bit of a challenge to call as
I've been mangled!
 >>>

So, the idea of single and double underscores is to convey in strong, 
or really strong, terms that the method probably ought not be called 
outside the class. One is mere convention, the other is convention 
plus a barrier in your way. But, if you want to eschew convention you 
can -- "we are all adults here."


Best,

Brian vdB




More information about the Tutor mailing list