Constructor of object

Bruno Desthuilliers bdesth.quelquechose at
Wed Mar 14 20:41:01 CET 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:

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 

More information about the Python-list mailing list