473,407 Members | 2,359 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,407 software developers and data experts.

Very practical question

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
9 1254
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
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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mike | last post by:
Related to another topic I just posted, I wanted to discuss ways to optimize the validation of very large (>100MB) XML documents. First, I have no idea if something like this already exists; it...
14
by: Matt | last post by:
Hi folks. Can you help with some questions? I gather that some types supported by g++ are nonstandard but have been proposed as standards. Are the long long and unsigned long long types still...
30
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
4
by: Piedro | last post by:
Hi group, as I was browsing the web and checking out w3schools.com I read that the font tag was deprecated and that I should use css styles for formatting like for example <p class="p1"> Text...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
2
by: VMI | last post by:
I'm working with the CSLA and a big part of this framework is .Net remoting, which I would like to understand. What would a practical use of .Net remoting be? I read several descriptions, but I...
1
by: Mark | last post by:
Hi, I want to learn multi-threding in C++. After serching in amazon, I still cant decided which one i should buy. Anyone have suggestion of a book on practical threading in C++? Or is that any...
10
by: Peter Duniho | last post by:
This is kind of a question about C# and kind of one about the framework. Hopefully, there's an answer in there somewhere. :) I'm curious about the status of 32-bit vs 64-bit in C# and the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.