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.
[color=blue][color=green][color=darkred]
>>> class Point:[/color][/color][/color]
.... x = 0.0
.... y = 0.0
....[color=blue][color=green][color=darkred]
>>> def initializePoint (a,b):[/color][/color][/color]
.... p = Point()
.... p.x = a
.... p.y = b
.... return p
....[color=blue][color=green][color=darkred]
>>> class Ppoint(Point):[/color][/color][/color]
.... def __init__(self,a ,b):
.... self = initializePoint (a,b)
....[color=blue][color=green][color=darkred]
>>> p = initializePoint (1.0,2.0)
>>> p.x[/color][/color][/color]
1.0[color=blue][color=green][color=darkred]
>>> p.y[/color][/color][/color]
2.0[color=blue][color=green][color=darkred]
>>> p = Ppoint(1.0,2.0)
>>> p.x[/color][/color][/color]
0.0[color=blue][color=green][color=darkred]
>>> p.y[/color][/color][/color]
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
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.
[color=blue][color=green][color=darkred]
>>> class Point:[/color][/color][/color]
.... x = 0.0
.... y = 0.0
....[color=blue][color=green][color=darkred]
>>> def initializePoint (a,b):[/color][/color][/color]
.... p = Point()
.... p.x = a
.... p.y = b
.... return p
....[color=blue][color=green][color=darkred]
>>> class Ppoint(Point):[/color][/color][/color]
.... def __init__(self,a ,b):
.... self = initializePoint (a,b)
....[color=blue][color=green][color=darkred]
>>> p = initializePoint (1.0,2.0)
>>> p.x[/color][/color][/color]
1.0[color=blue][color=green][color=darkred]
>>> p.y[/color][/color][/color]
2.0[color=blue][color=green][color=darkred]
>>> p = Ppoint(1.0,2.0)
>>> p.x[/color][/color][/color]
0.0[color=blue][color=green][color=darkred]
>>> p.y[/color][/color][/color]
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
Comment