Simple question about variables

laotseu bdesth at nospam.free.fr
Wed Dec 4 22:22:22 EST 2002


John Hunter wrote:
>>>>>>"Michel" == Michel COMBE <michel.l.combe at free.fr> writes:
>>>>>
> 
>     Michel> I would like the 3 phases to be 3 procedures.  How can I
>     Michel> manage to have the record set declared globally so that I
>     Michel> can use it from all 3 procedures ?  (Right now, it is
>     Michel> declared locally in the first procedure by the statement
>     Michel> result=cursor.fetchall )
> 
> You could also wrap the 3 procedures in a class and use attributes to
> share data
> 
> class myclass:
> 
>   def __init__(self):
>     self._var = None
> 
>   def proc1(self):
>     self._var = do_something
> 
>   def proc2(self):
>     do_something_else_with self._var
> 
>   
> 

<IMHO>
Yep, but you come with the same problem that make globals bad : you 
can't say if myclass.proc1() has been called before myclass.proc2() is. 
I mean that instances attributes are somehow global to a set of 
functions for a particular piece of data, and methods relies on (or must 
check for...) a particular state. While this may not be a reason  not to 
use oop (which I really like), I think it's better to still avoid using 
'state variables' when they are not necessary. And there's only one case 
where globals are necessary : when a piece of data must be shared by two 
functions and none of them calls the other, directly or not.

So even in an oop program, I'd still prefer a solution like 
fun3(fun2(fun1(base_data))).
</IMHO>

Laotseu




More information about the Python-list mailing list