By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,496 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

Very practical question

P: n/a
I've been doing an application with Tkinter widgets. Nothing really
fancy just routine stuff. Though I have no problems with it by now I
guess it would be reasonable to ask about a thing that's been bothering
me a bit. Look at this piece of code:

class A(object):
def a(self):
return "a from A"

class B(object):
def interClassCall(self):
print globals()['c'].__dict__['a'].a()

class C(object):
def __init__(self):
self.a=A()
self.b=B()
def c(self):
self.b.interClassCall()

if __name__=="__main__":
c=C()
c.c()

What is another way to get data from method of another instance of a
class? Or maybe print globals()['c'].__dict__['a'].a() is perfectly
normal. I need your professional expertise.

Jul 5 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
madpython wrote:
What is another way to get data from method of another instance of a
class? Or maybe print globals()['c'].__dict__['a'].a() is perfectly
normal.
I'd say it's a fireable offense.

</F>

Jul 5 '06 #2

P: n/a
In <11**********************@p79g2000cwp.googlegroups .com>, madpython
wrote:
I've been doing an application with Tkinter widgets. Nothing really
fancy just routine stuff. Though I have no problems with it by now I
guess it would be reasonable to ask about a thing that's been bothering
me a bit. Look at this piece of code:

class A(object):
def a(self):
return "a from A"

class B(object):
def interClassCall(self):
print globals()['c'].__dict__['a'].a()

class C(object):
def __init__(self):
self.a=A()
self.b=B()
def c(self):
self.b.interClassCall()

if __name__=="__main__":
c=C()
c.c()

What is another way to get data from method of another instance of a
class? Or maybe print globals()['c'].__dict__['a'].a() is perfectly
normal. I need your professional expertise.
No it's not the normal way. Why don't you give `c` as argument to the
`interClassCall()`?

class B(object):
def interClassCall(self, c):
print c.a.a()

class C(object):
def __init__(self):
self.a=A()
self.b=B()
def c(self):
self.b.interClassCall(self)

Much less magic involved this way.

Ciao,
Marc 'BlackJack' Rintsch
Jul 5 '06 #3

P: n/a
madpython a écrit :
I've been doing an application with Tkinter widgets. Nothing really
fancy just routine stuff. Though I have no problems with it by now I
guess it would be reasonable to ask about a thing that's been bothering
me a bit. Look at this piece of code:

class A(object):
def a(self):
return "a from A"

class B(object):
def interClassCall(self):
print globals()['c'].__dict__['a'].a()
Yuck. You're fired.
class C(object):
def __init__(self):
self.a=A()
self.b=B()
def c(self):
self.b.interClassCall()

if __name__=="__main__":
c=C()
c.c()

What is another way to get data from method of another instance of a
class?
Have a reference to that instance. Usually by one of the following method:
1/ create the C instance in the calling method
2/ have the C instance passed as an argument to the calling method
3/ have the C instance as an attribute of the B instance
4/ get the C instance from a module function (but then you have to solve
how this module function itself gets the C instance, see above)

In your case, since it's the C instance that calls the B method that
needs a reference to the C instance, just have the C instance pass
itself to B:

class B(object):
def interClassCall(self, c):
print c.a.a()

class C(object):
def __init__(self):
self.a=A()
self.b=B()
def c(self):
self.b.interClassCall(self)

Now, since B.interClassCall() doesn't need c, but the result of the call
to c.a.a(), it would be better to directly pass that result:

class B(object):
def interClassCall(self, something):
print something

class C(object):
def __init__(self):
self.a=A()
self.b=B()
def c(self):
self.b.interClassCall(self.a.a())

Or maybe print globals()['c'].__dict__['a'].a() is perfectly
normal.
Definitively not. Not only is it a very complicated way to write a
simple thing, but it's also totally defeating the whole point of using
objects.
I need your professional expertise.
It doesn't require any professional expertise to see what's wrong with
your code. It's CS101.
Jul 5 '06 #4

P: n/a
Marc 'BlackJack' Rintsch wrote:
In <11**********************@p79g2000cwp.googlegroups .com>, madpython
wrote:
No it's not the normal way. Why don't you give `c` as argument to the
`interClassCall()`?

class B(object):
def interClassCall(self, c):
print c.a.a()

class C(object):
def __init__(self):
self.a=A()
self.b=B()
def c(self):
self.b.interClassCall(self)

Much less magic involved this way.
As far as I remember what you suggest would be perfect solution for
Java. In other words it's a matter of getting a reference to "a"
(the instance of class A) and your variant just one of the others
possible. Maybe I came up with not very good example, so maybe I try to
explain it verbally.
As I already have written I play with Tkinter and make an application
that does some stuff. It uses dynamic GUI building depending on data it
process. Also one of the requriments is to separate the program logic
from GUI.
Here is a short illustration:

....
self.b=Tkinter.Button(root,txt="Button",command=se lf.doSmth).pack()
self.l=Tkinter.Label(root,txt="default").pack()
def doSmth(self):
var=globals()["m"].__dict__["progLogic"].func("some
input")
self.l.config(txt=var)
self.l.update_idletasks()
....
I guess it's all correct or at least it close to what I work on. What
do you think? If I may I'd say it again that GUI is built according by
the data that's passed by the "thinking" part of the program so I
don't necessary know what it is (can only guess) and that's why passing
references as an argument doesn't seem possible.

Jul 5 '06 #5

P: n/a
madpython a écrit :
Marc 'BlackJack' Rintsch wrote:
>>In <11**********************@p79g2000cwp.googlegroups .com>, madpython
wrote:
No it's not the normal way. Why don't you give `c` as argument to the
`interClassCall()`?

class B(object):
def interClassCall(self, c):
print c.a.a()

class C(object):
def __init__(self):
self.a=A()
self.b=B()
def c(self):
self.b.interClassCall(self)

Much less magic involved this way.

As far as I remember what you suggest would be perfect solution for
Java.
And for any other language, be it OO, functional or strictly procedural.
In other words it's a matter of getting a reference to "a"
(the instance of class A) and your variant just one of the others
possible. Maybe I came up with not very good example, so maybe I try to
explain it verbally.
As I already have written I play with Tkinter and make an application
that does some stuff. It uses dynamic GUI building depending on data it
process. Also one of the requriments is to separate the program logic
from GUI.
Separating program logic from presentation is not excuse to write bad code.
Here is a short illustration:

...
self.b=Tkinter.Button(root,txt="Button",command=se lf.doSmth).pack()
self.l=Tkinter.Label(root,txt="default").pack()
def doSmth(self):
var=globals()["m"].__dict__["progLogic"].func("some
input")
Yuck++
self.l.config(txt=var)
self.l.update_idletasks()
...
I guess it's all correct
It's an horror.
or at least it close to what I work on. What
do you think?
I think that it's an horror.
If I may I'd say it again that GUI is built according by
the data that's passed by the "thinking" part of the program
It's not "passed".
so I
don't necessary know what it is
What is polymorphism for ?
(can only guess)
You don't have anything to guess. Just to learn what "design" means.
and that's why passing
references as an argument doesn't seem possible.
Then you have a problem with your design. FWIW, decoupling presentation
from program logic is nothing new - it has been solved *many* years ago.
Google for MVC.

Jul 5 '06 #6

P: n/a
"madpython" <ma*******@gmail.comwrote:
>I've been doing an application with Tkinter widgets. Nothing really
fancy just routine stuff. Though I have no problems with it by now I
guess it would be reasonable to ask about a thing that's been bothering
me a bit. Look at this piece of code:

class A(object):
def a(self):
return "a from A"

class B(object):
def interClassCall(self):
print globals()['c'].__dict__['a'].a()
The others have given you good advice about better ways to do this, but I'd
like to point out that this one line is equivalent to:

print c.a.a()
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 6 '06 #7

P: n/a
madpython wrote:
Here is a short illustration:

...
self.b=Tkinter.Button(root,txt="Button",command=se lf.doSmth).pack()
self.l=Tkinter.Label(root,txt="default").pack()
def doSmth(self):
var=globals()["m"].__dict__["progLogic"].func("some
input")
self.l.config(txt=var)
self.l.update_idletasks()
...
I guess it's all correct or at least it close to what I work on. What
do you think?
what makes you think that

var=globals()["m"].__dict__["progLogic"].func("some input")

is, in any way, different from (and superior to)

var = m.progLogic.func("some input")

?
If I may I'd say it again that GUI is built according by
the data that's passed by the "thinking" part of the program so I
don't necessary know what it is (can only guess) and that's why
passing references as an argument doesn't seem possible.
sorry, but you make no sense at all. solving this is not a hard pro-
blem, and the solution doesn't need to involve global variables. and
even if you prefer globals, there's no need to write ludicrous code.

</F>

Jul 6 '06 #8

P: n/a
Thank you all for your comments. They are priceless beyond any doubt.
As for the matter of the discussion it took me only a minute looking at
the code to realize that with Tkinter I pass "master" reference to
every widget and therefore I can access every method in the class
hierarchy. I'm a fool that I haven't noticed it earlier.

Jul 6 '06 #9

P: n/a
madpython wrote:
...
self.b=Tkinter.Button(root,txt="Button",command=se lf.doSmth).pack()
self.l=Tkinter.Label(root,txt="default").pack()
def doSmth(self):
var=globals()["m"].__dict__["progLogic"].func("some
input")
self.l.config(txt=var)
self.l.update_idletasks()
...

pack() method returns None, *not* the packed widget.
>>import Tkinter
b = Tkinter.Button()
n = b.pack()
n
print n
None

Jul 6 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.