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

initialize a class

P: n/a
Hi,

I am trying to construct a 'point' instance with two different methods (see
the following codes). Why the second one can't work? It's strange, since
'self' refers to the newly created object, and the '=' assign a initialized
instance to it.
class Point: .... x = 0.0
.... y = 0.0
.... def initializePoint(a,b): .... p = Point()
.... p.x = a
.... p.y = b
.... return p
.... class Ppoint(Point): .... def __init__(self,a,b):
.... self = initializePoint(a,b)
.... p = initializePoint(1.0,2.0)
p.x 1.0 p.y 2.0 p = Ppoint(1.0,2.0)
p.x 0.0 p.y

0.0

Acutally, what I want to do is like the code above: I have a huge class in a
module, and a function to initialize instances of this class. But I want the
instance of the class to be initialized when it's created. How can I fulfill
such a task? Thanks!

Regards,
Yang
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In article <ma************************************@python.org >, myang wrote:
Hi,

I am trying to construct a 'point' instance with two different methods (see
the following codes). Why the second one can't work? It's strange, since
'self' refers to the newly created object, and the '=' assign a initialized
instance to it.
class Point: ... x = 0.0
... y = 0.0
... def initializePoint(a,b): ... p = Point()
... p.x = a
... p.y = b
... return p
... class Ppoint(Point):
... def __init__(self,a,b):
... self = initializePoint(a,b)


By the time it calls __init__, the Ppoint object is already created, and
a reference to it is passed in to the function in the self parameter.
What your code does is calls initializePoint, which creates a second
object, and stores a reference to *that* in self. Then it throws it
away, since self is returned. The original Ppoint object isn't touched.

The standard way to write an initializer is this:

class Point:
def __init__(self, a = 0.0, b = 0.0):
self.x = a
self.y = b

def initializePoint(a, b):
p = Point(a, b)
return p

Since __init__ has default values, ig you just call Point(), you get 0.0
as before. But now initializePoint() isn't very useful, and only exists
to keep your old code working.
Acutally, what I want to do is like the code above: I have a huge class in a
module, and a function to initialize instances of this class. But I want the
instance of the class to be initialized when it's created. How can I fulfill
such a task? Thanks!


If you're allowed to change the class, just move initializeC to
C.__init__, as above.

If you're not allowed to change the Point class, but you can change the
initialize function, you can do this:

class Point:
x = 0.0
y = 0.0

def __initializePoint(p, a, b):
p.x = a
p.y = b

def initializePoint(a, b):
p = Point()
__initializePoint(p, a, b)
return p

class Ppoint(Point):
def __init__(self, a, b):
__initializePoint(self, a, b)

Now the two ways to create a point (Ppoint() and initializePoint()) both
create an object, then call the same initiailzation code.

If you can't change any of the code from the module, it's more complex.
Complain to whoever wrote it - why on Earth did they design it that way?

Joe
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.