[Tutor] Trying to find a value in a function.

D2 borelan@wanadoo.fr
Tue Mar 11 09:23:01 2003


Magnus Lycka a écrit:
> 
>> I'm trying to find a value in a fonction given the name of the function
>> and the name of the variable.
>> The variable is not an attribute nor an argument.
> 
> 
> You're not really supposed to do this... Can you
> tell us what it is you are trying to achieve? We
> might suggest a better approach.
> 
> A variable has variable values, that vary over time.
> Python functions are not static, they don't keep any
> values in their local scope until you run them. In
> other words, props inside some_func only exists when
> you run some_func, and then the value is initially
> undefined, and later ['a', 'b', 'c'].
> 
> I don't see any other way to get what you want without
> calling some_func than to get to the source code of
> it, and then do the same text processing as with any
> other text file.

That's the way i'm going but i thought there was a statement to get the 
description of an assignation in a function, without processing the source.
See my answer to Gregor Lingl and my answer to your message in 
newbie-class differs from function. (sorry, i got your mail after having 
posted my comment).



> 
> To keep context in variables between function calls,
> create a class, and make the values you need to persist
> attributes in the class.

That's the way i'm doing it. I use mixin classes too as you describe 
them in your class explanation.

Andre, waiting for an advice.

> 
>> Here is the function
>>
>> ---------
>> def some_func(self):
>>         props=['a', 'b', 'c']
>> ---------
>>
>> I tried to use some_func.func_code.co_varnames but it only returned the
>> tuple below.
>>
>> ---------
>> ('self', '_props')
>> ---------
>>
>> How can i get the value of the _props variable ?
> 
> 
>