Constructor of object

inline rodin.alexander at gmail.com
Wed Mar 14 14:45:35 EDT 2007


On Mar 14, 9:05 pm, Thinker <thin... at branda.to> wrote:
> -----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 - thin... at branda.to thinker... at gmail.comhttp://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-----

I know it, but i don't want call constructor of parent class - I use
PyGTK and want to load window from glade file and assign it to object,
parented gtk.Window:

#!/usr/bin/env python

import gtk
import gtk.glade

class HelloWindow (gtk.Window):
    def __init__(self):
        xml = gtk.glade.XML("hello.glade")
        xml.signal_autoconnect(self)
        self = xml.get_widget("window")

window = HelloWindow()
window.connect("delete_event", gtk.main_quit)
window.show_all()
gtk.main()




More information about the Python-list mailing list