how to use private method in a class

W

wang frank

Hi,

I am trying to write a python class with a new data type such as:
class Cc14:
def __init__(self, realpart, imagpart):
self.r=realart
self.i=imagpart

def __saturator(x):
return x+1
def out(self,x):
return Cc14(__saturator(x.r), __saturator(x,i))

When I use the method out such as:
z.out

Python complains:

global name '_Cc14_saturator' is not defined.

Is the way put two underscore in front of the definitio making the method
becomes private?

Why in the same clase, I could not use the __saturator method?

Thanks

Frank


From: "wang frank" <[email protected]>
To: (e-mail address removed)
Subject: A newbie question
Date: Mon, 21 May 2007 23:04:06 +0000

Hi,

I am trying to write a python class with a new data type such as:
class Cc14:
def __init__(self, realpart, imagpart):
self.r=realart
self.i=imagpart

def __add__(self,x):
return self.r+x,r, self.i+x.i

If I have
x=Cc14(4,5)
y=Cc14(4,5)
z=x+y

z will be a tuple instead of Cc14. How can I return a Cc14 class?

Thanks
Frank

_________________________________________________________________
$B%&%'%V%Z!<%8$r0u:~$7$F$bES@Z$l$J$$!*JXMx$J%V%i%&%6(B MSN$BHG(BIE7 $B$r;H$*$&(B
http://promotion.msn.co.jp/ie7/

_________________________________________________________________
$B%*%s%i%$%sCO?^%^%,%8%s!VCO?^%^%,!WAO4)!*A49q?eB24[%^%C%W$rFC=8(B
http://chizumaga.jp/
 
B

Basilisk96

Hi,

I am trying to write a python class with a new data type such as:
class Cc14:
def __init__(self, realpart, imagpart):
self.r=realart
self.i=imagpart

def __saturator(x):
return x+1
def out(self,x):
return Cc14(__saturator(x.r), __saturator(x,i))

When I use the method out such as:
z.out

Python complains:

global name '_Cc14_saturator' is not defined.

Is the way put two underscore in front of the definitio making the method
becomes private?

Why in the same clase, I could not use the __saturator method?

Thanks

Frank

It seems you have several issues here:

(1) To avoid syntax errors,
self.r=realart
should be:
self.r=realpart

(2) Your __saturator method definition needs the reference to self:
def __saturator(self, x):

(3) And lastly, the body of the out() method needs two corrections:
return Cc14(self.__saturator(x.r), self.__saturator(x.i))

Is it really necessary to "privatize" the saturator method? In Python,
all class methods are public and visible by default. A method name
__methodname in a class "foo" turns into "_foo__methodname" in an
instance of the class. This is known as name mangling. It is simply a
convention supported by Python when a class attribute name begins with
two underscores. It prevents a programmer from calling something like
C.__saturator(arg), but still allows calling C._Cc14__saturator(arg).

IMHO, you could just leave the saturator definition as "def
saturator(self, x):"

Anyway, in your case the 3 fixes above will allow you now to do this:
(3, 4)

Which is beginning to look like your design intent..

Cheers,
-Basilisk96
 

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
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top