473,287 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Constructor of object

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?

Mar 14 '07 #1
7 1642
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?
You want to override the __new__ function.
Mar 14 '07 #2
-----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 - th*****@branda.to th********@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-----

Mar 14 '07 #3
On Mar 14, 9:05 pm, Thinker <thin...@branda.towrote:
-----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...@branda.to thinker...@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()

Mar 14 '07 #4
On Mar 14, 9:05 pm, Thinker <thin...@branda.towrote:
-----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...@branda.to thinker...@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()

Mar 14 '07 #5
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/relea...intro/#__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...
Mar 14 '07 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

inline wrote:
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 - th*****@branda.to th********@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+FZH1LDUVnWfY8gRAv/yAKDz6VWmIJkLmQ72UwZLBF/hrHUPPgCePRBq
jSx5PySyDc/pW3SOqZEFM/I=
=T746
-----END PGP SIGNATURE-----

Mar 14 '07 #7
Thanks. But can I change returned by xml.get_widget() object type from
gtk.Window to HelloWindow?

Mar 15 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
4
by: Tony Johansson | last post by:
Hello Experts! I have this constructor for class Flight. Flight::Flight(string flight_no, Klocka dep_t, Klocka arr_t) : no(flight_no), dep(dep_t), arr(arr_t) {} Both dep and arr are...
2
by: vakap | last post by:
function show() { var s = '' ; for (var i = 0; i<arguments.length; s += '\n'+arguments) ; typeof(window) != 'undefined' ? window.alert(s) : WScript.Echo(s) ; } function f(){}...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
12
by: satyajit | last post by:
I am trying to learn the concept of constructors in ECMAScript. I executed following code (See execution in Rhino JavaScript shell): function Foo(a) { this.a = a; } function Bar(b) { this.b...
13
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.