initialize a class

myang myang at clarku.edu
Sun Mar 28 00:43:24 CET 2004


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





More information about the Python-list mailing list