Constructor of object

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Mar 14 15:41:01 EDT 2007


inline a écrit :
> Hello!
> I want to assign self to object of parent class in constructor,

Isn't it the other way round ?-)

> like
> 
> def my_func():
>     ...
>     return ParentClass()
> 
> class MyClass (ParentClass):
>     def __init__(self):
>         self = my_func()

First point : __init_ is *not* the constructor. It's the initializer. 
The constructor is named __new__. You'll find more about __new__ here:

http://www.python.org/download/releases/2.2.3/descrintro/#__new__

Second point: 'self' is just an ordinary local variable. So rebinding it 
has no effect outside the function.

> but it not work, because "object not initialized". What i can do?

Learn to post a full traceback ?-)
(Sorry, just kidding.)

FWIW, I don't know which problem you're trying to solve this way, but 
there may (conditional...) be better solutions. Like using a factory 
function...



More information about the Python-list mailing list