Two classes problem

Caleb Hattingh caleb1 at telkomsa.net
Thu Feb 3 23:28:45 EST 2005


Gurpreet

You can manage the namespace more formally. Or to put it another way,  
"global" gives me the heebie-jeebies.  I recently worked on a project  
replacing a legacy reactor model in FORTRAN, and between COMMON blocks,  
and GOTO statements, I didn't know up from down.

How about this:

***
class a:
       def __init__(self,test):
              localtest = test
              # do stuff with localtest
       def givetest(self):
              return localtest
       def printtest(self):
              print localtest

test = 'Starting text'
print 'Outside before class: '+test
my_a = a(test)
test = my_a.givetest()
print 'Outside after class: '+test
***

So here we explicitly pass "test" into the class, do stuff with it, and  
rewrite test again with a method.  Does this satisfy the technical problem?

regards
Caleb




On Thu, 3 Feb 2005 11:19:34 +0530, Gurpreet Sachdeva  
<gurpreet.sachdeva at gmail.com> wrote:

> The purpose is, I pass a list to a class in a module but I want to use
> that list out of the scope of that class and that too not in any other
> class or a function but in the main program...
> The problem is that when I import that, the statements in the module
> which are not in the class are executed first and then the variable
> gets intiallised...
> I will explain with the example...
>
> -global test
> -
> -class a:
> -        def __init__(self,test):
> -                global test
> -                print test
> -
> -print 'Outside: '+test
>
> I want to print that variable test which I am giving to the class as
> an argument, in the scope of main...
> I know it is not a good way of programming but my situation is like  
> this...
> But is this possible or not? If I pass test as 'Garry' can I (by any
> way) print 'Outside: Garry' with that print statement... (in the main
> scope)
>
> Thanks and Regards,
> Garry




More information about the Python-list mailing list