Two classes problem
Caleb Hattingh
caleb1 at telkomsa.net
Wed Feb 2 22:08:36 EST 2005
Hi
It would help if you could describe the purpose you have in mind for doing
this. There is a cute way of doing what you want:
===file: a.py===
# module a.py
test = 'first'
class aclass:
def __init__(self, mod, value):
mod.test = value # Is there another way to refer to
the module this class sits in?
===end: a.py===
===file: b.py===
# file b.py
import a
x = a.aclass(a,'monkey')
print a.test
===end: b.py===
When you run "b.py", you will see 'monkey' printed. What we have done
here is passed the reference to the *module* a to the constructor for
aclass, and that constructor modified the module variable "test". This is
a way to avoid using 'global', or in other words, the namespaces of things
are still clear (for me anyway).
Cya
Caleb
On Thu, 3 Feb 2005 00:13:05 +0530, Gurpreet Sachdeva
<gurpreet.sachdeva at gmail.com> wrote:
> I have two classes;
>
> a.py -->
>
> #!/usr/bin/python
> global test
> test =''
> class a(b):
> def __init__(self,test):
> print test
> print 'Outside: '+test
>
> b.py -->
>
> #!/usr/bin/python
> import a
> a.a('Garry')
>
> I want to pass this string (or any object later) to a.py and that too
> outside the scope of class a.
> Is that possible??? If yes, how???
>
> Thanks,
>
> Garry
More information about the Python-list
mailing list