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? 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.
-----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-----
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()
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()
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...
-----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-----
Thanks. But can I change returned by xml.get_widget() object type from
gtk.Window to HelloWindow? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jun |
last post by:
I have following script
<script>
var Animal = function(name){
this.name = name;
}
Animal.prototype.eat = function (food)
{
|
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) .
|
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...
|
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...
|
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(){}...
|
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...
|
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);...
|
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...
|
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");
}
|
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...
|
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...
|
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"....
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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)...
| |