function call problem in class?

D

Davy

Hi all,

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()

Any suggestion is welcome!
Best regards,
Davy
 
G

Gabriel Genellina

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).
 
D

Davy

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()
Hi Gabriel,

Thank you. now my code work well )

Davy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top