Very practical question

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Jul 5 13:50:05 EDT 2006


In <1152117913.457095.306480 at 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



More information about the Python-list mailing list