You are not logged in.

#1 2007-02-17 20:38:56

pelle.k
Member
From: Åre, Sweden (EU)
Registered: 2006-04-30
Posts: 667

OO python - access instance in which object was created?

I'm playing with python and qt a bit. Just for fun. To clarify what i meant with the title of this thread, i'll elaborate on what i'm doing.

I've imported one "mainwindow" widget, and one "regular" widget constructed in qt designer.
The idea is to have a listview in the "mainwidget", and use the regular "widget" as a modal dialog to modify the columns in the "mainwidget".
To do this, this is how it is constructed (roughly);

class applicationForm(QApplication):
this is where i create self.form (from appForm), show it and exec it.
class appForm(mainwindow):
this is where the action occurrs. I create the instance self.dialog (from appDialog) and exec it, and i also reference the listview (self.dialog.listview) since i can't access it from within self.dialog otherwise right? This is a bit clunky since i would like to have access to the whole self.form instance instead. I could reference it from the former class (and reference that to self.dialog), but isn't there a better way to do this?
class appDialog(regularwindow):
This is the modal dialog. Another strange behaviour is that i can't access the (self.dialog.listview i referenced) with self.listview from the __init__ of this clas. So i have to modify the widgets (which need input from the self.listview) to be of any use, _before_ i exec this dialog window in the self.form instance. I can however access self.listview from the methods i've tied with signals from the GUI.

Ideas, suggestions? Am I attacking this little program of mine from the wrong angle? What about the layout?
btw, if my questions are a bit strange - it's because i'm an OOP noob, and i've not used python very much before.


"Your beliefs can be like fences that surround you.
You must first see them or you will not even realize that you are not free, simply because you will not see beyond the fences.
They will represent the boundaries of your experience."

SETH / Jane Roberts

Offline

#2 2007-06-07 23:46:19

pelle.k
Member
From: Åre, Sweden (EU)
Registered: 2006-04-30
Posts: 667

Re: OO python - access instance in which object was created?

OK, let me rephrase this;
lets say i use wxPython.

3. I have one instance of a class derived from wx.Dialog (modal)
2. in the instance of a class derived from wx.Frame
1. in an instance of a class derived from wx.App

i would like to modify attributes in instance nr 2, but I don't know how to access them from the class of nr 3 other than importing like this;

class dialog(wx.Dialog):
    <init>
    ........
    self.dothis() ### this is only being run by an event handler... no need to illustrate that
    ........

class frame(wx.Frame):
    <init>
    def dothis():
         pass
    newdialog = dialog(self, variables)
    newdialog.dothis = self.dothis ### this is the ugly import....

class app(wx.App):
    <init and stuff>
    mainframe = frame
    mainframe.show

application = app(variables)
application.run

This is not _actual_ code as you see, but just a replica of what i'm trying to do.

Last edited by pelle.k (2007-06-07 23:48:31)


"Your beliefs can be like fences that surround you.
You must first see them or you will not even realize that you are not free, simply because you will not see beyond the fences.
They will represent the boundaries of your experience."

SETH / Jane Roberts

Offline

#3 2007-06-08 04:33:04

gradgrind
Member
From: Germany
Registered: 2005-10-06
Posts: 921

Re: OO python - access instance in which object was created?

I guess I don't really understand the question, but if you want to modify things in frame from code in dialog, why don't you just pass the frame instance to the dialog? Then the dialog can do what it likes with the frame instance.

From an OO point of view, however, it might be better to provide methods in frame to perform the desired manipulations. These methods can then be called from dialog.

Offline

#4 2007-06-08 18:17:00

pelle.k
Member
From: Åre, Sweden (EU)
Registered: 2006-04-30
Posts: 667

Re: OO python - access instance in which object was created?

Well, i guess this isn't specific to wxPython/Qt or any toolkit, but rather python itself.

I guess you meant that i should pass the whole <frame class> as a reference to the <dialog class instance> like this;

class dialog():
    ...
    self.frameRef.dothis() ## this runs dothis method in "frame"
    ...

class frame():
    ...
    def dothis(self, whatever):
        print('hello from dialog')
    self.mydialog = dialog()
    self.mydialog.frameRef = self
    ....

class app():
    ....
    self.one = frame()
    self.two = frame()
    ....

Feels a little bit odd, but it does work!
What about memory consumption when passing a whole <class reference> to an <class instance> just like that (i mean, maybe i have to do this more than one in an application)?


"Your beliefs can be like fences that surround you.
You must first see them or you will not even realize that you are not free, simply because you will not see beyond the fences.
They will represent the boundaries of your experience."

SETH / Jane Roberts

Offline

#5 2007-06-09 05:06:59

gradgrind
Member
From: Germany
Registered: 2005-10-06
Posts: 921

Re: OO python - access instance in which object was created?

Yes, that's what I meant - apart from the "print('hello from dialog')", which of course should not be in class frame!

class dialog():
    ...
    self.frameRef.dothis('hello from dialog') ## this runs dothis method in "frame"
    ...

class frame():
    ...
    def dothis(self, whatever):
        print whatever
    ....

But maybe that's just being pedantic...

Memory consumption? One reference to an instance? That is really negligible, it's not as if the whole object were being copied.

Offline

#6 2007-06-09 14:06:18

pelle.k
Member
From: Åre, Sweden (EU)
Registered: 2006-04-30
Posts: 667

Re: OO python - access instance in which object was created?

But maybe that's just being pedantic...

Pedantic is good. I'm trying to learn how to do things the right way before i learn bad practices. (getting rid of procedural thinking in python is more than enough...)
Thanks! smile


"Your beliefs can be like fences that surround you.
You must first see them or you will not even realize that you are not free, simply because you will not see beyond the fences.
They will represent the boundaries of your experience."

SETH / Jane Roberts

Offline

Board footer

Powered by FluxBB