function call problem in class?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Nov 14 03:10:16 EST 2007


En Wed, 14 Nov 2007 04:51:57 -0300, Davy <zhushenli at gmail.com> escribió:

> I have write a simple class, I want the function two() to call private
> function __one(), but there is an error :
> NameError: global name '_simple__one' is not defined, how to work
> around it
>
> class simple:
>     def __one(self):
>         print "Hello"
>     def two(self):
>         __one()
>         print "world"
>
> if __name__ == '__main__':
>     s = simple()
>     s.two()

Note that your problem is not related to mangled names: replacing __one by  
one raises a similar exception.
Remember that "self" is not implicit: you must use self.__one()

"private" methods (and atributes in general) use a single underscore:  
_one. Double underscores __one are reserved for the (rare) cases when you  
want to ensure unique names (or name clashes are expected).

-- 
Gabriel Genellina




More information about the Python-list mailing list