473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to use private method in a class

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" <fw*@hotmail.co.jp>
To: py*********@python.org
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/

--
http://mail.python.org/mailman/listinfo/python-list
__________________________________________________ _______________
$B%*%s%i%$%sCO?^%^%,%8%s!VCO?^%^%,!WAO4)!*A49q?eB 24[%^%C%W$rFC=8(B
http://chizumaga.jp/

May 22 '07 #1
1 2978
On May 21, 9:49 pm, "wang frank" <f...@hotmail.co.jpwrote:
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:
>>>C = Cc14(2,3)
result = C.out(C)
result.r, result.i
(3, 4)

Which is beginning to look like your design intent..

Cheers,
-Basilisk96

May 22 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
5
by: David Rubin | last post by:
Is there ever a good reason to declare private non-virtual member functions in a class definition? As far as private virtual function are concerned, my understanding is that, if you have a...
3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
10
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
9
by: Ken Varn | last post by:
Is there anyway to override a public virtual method or property so that it is private in my derived class? I tried using new on the property and making it private, but no luck. --...
6
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int...
2
by: Christoph Boget | last post by:
Let's take the following class: class MyClass { private int privateVar; public int PublicVar { get { return privateVar; } } public MyClass() {}
5
by: TomislaW | last post by:
What is the purpose or difference between private static and private method in non-static class?
7
by: Charles Law | last post by:
Sorry, but I am on a bit of a roll today. I have just found that I can make a method private to my class, but then use AddHandler in an external class to hook it up. Is that what people would...
6
by: WXS | last post by:
I know this sounds contrary to the idea of an interface, but read this and see what you think. ----------------------------------------------------------------------------------------- It would be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.