By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,400 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,400 IT Pros & Developers. It's quick & easy.

function call problem in class?

P: n/a
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

Nov 14 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
En Wed, 14 Nov 2007 04:51:57 -0300, Davy <zh*******@gmail.comescribió:
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

Nov 14 '07 #2

P: n/a
On Nov 14, 4:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 14 Nov 2007 04:51:57 -0300, Davy <zhushe...@gmail.comescribió:
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()
Hi Gabriel,

Thank you. now my code work well )

Davy
>
"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

Nov 14 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.