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

Magnus Lycka magnus@thinkware.se
Tue Mar 11 06:39:01 2003


>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.

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

>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 ?


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se