Constructor of object

Thinker thinker at branda.to
Wed Mar 14 14:05:33 EDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

inline wrote:
> Hello! I want to assign self to object of parent class in
> constructor, like
>
> def my_func(): ... return ParentClass()
>
> class MyClass (ParentClass): def __init__(self): self = my_func()
>
> but it not work, because "object not initialized". What i can do?
>
Do you want to call constructor of super-class to initialize the object?
If it is true, you have following options.

In old style class:
class MyClass(ParentClass):
def __init__(self):
ParentClass.__init__(self)

In new style class:
class MyClass(ParentClass):
def __init__(self):
super(MyClass).__init__(self)

Or
class MyClass(ParentClass):
def __init__(self):
super(MyClass, self).__init__()

- --
Thinker Li - thinker at branda.to thinker.li at gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF+Dls1LDUVnWfY8gRAvDaAKDVmX8LmuWUdJ4eVil7l//rjCQZLQCg8dO8
Y77CL1ikmtdl6S3HD04GWiA=
=mvSe
-----END PGP SIGNATURE-----




More information about the Python-list mailing list