Constructor of object

I

inline

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?
 
S

Sylvain Defresne

inline said:
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?

You want to override the __new__ function.
 
T

Thinker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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 - (e-mail address removed) (e-mail address removed)
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-----
 
I

inline

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





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 - (e-mail address removed) (e-mail address removed)://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()
 
I

inline

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





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 - (e-mail address removed) (e-mail address removed)://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()
 
B

Bruno Desthuilliers

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...
 
T

Thinker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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()

Why don't you just define HelloWindow as a function? It is more simple.
If you want HelloWindow() to return the object from glade, you can
make HelloWindow as a function or
a class with __new__() method, suggested by Sylvain's, that return a
object from glade.

for ex:
def HelloWindow():
.......
return xml.get_widget("window")
pass

or
class HelloWindow:
def __init__(self):
pass
def __new__(clz, *args):
.........
return xml.get_widget("window")

- --
Thinker Li - (e-mail address removed) (e-mail address removed)
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+FZH1LDUVnWfY8gRAv/yAKDz6VWmIJkLmQ72UwZLBF/hrHUPPgCePRBq
jSx5PySyDc/pW3SOqZEFM/I=
=T746
-----END PGP SIGNATURE-----
 
I

inline

Thanks. But can I change returned by xml.get_widget() object type from
gtk.Window to HelloWindow?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top