Gathering variable names from within a function.

POYEN OP Olivier (DCL) Olivier.POYEN at clf-dexia.com
Mon Jul 7 07:53:22 EDT 2003


> "Xavier" <sabu at pure-elite.org> wrote in message 
> news:<mailman.1057315927.28411.python-list at python.org>...
> > Greetings,
> > 
> > I was wondering if there was a way to get a list of all the 
> variables which
> > were executed/created/modified within a function?
> > 
> > Here is a simple example that comes to my mind:
> > 
> > def getVars(func):
> >     ...
> >     ...
> >     ...
> > 
> > def modVar(arg, arg2):
> > 
> >     Var1 = arg
> >     Var2 = arg2
> >     Var3 = arg+arg2
> >     return Var3
> > 
> > def main():
> > 
> >     print getVars(modVar('test', 'test2'))
> > 
> > # end
> > 
> > naturally, "Var1", "Var2" and "Var3" should be printed to 
> the user.  Any
> > idea?
> >
> 
> What about
>  
> def modVar(arg, arg2):
>     Var1 = arg
>     Var2 = arg2
>     Var3 = arg+arg2
>     print vars()
>     return Var3
> 
> modVar('test', 'test2')
> 
> =>
> 
> {'arg2': 'test2', 
> 'arg': 'test', 
> 'Var1': 'test', 
> 'Var3': 'testtest2', 
> 'Var2': 'test2'}
> 
> 

more cryptic, but available from outside modVar:

def getVars(func):
	return func.func_code.co_varnames


HTH,

--OPQ 
-------------- next part --------------
-----------------------------------------------------------------

Ce message est confidentiel ; son contenu ne represente en aucun

cas un engagement de la part de Dexia Credit Local ou de Dexia

CLF Banque. Toute publication, utilisation ou diffusion, meme

partielle, doit etre autorisee prealablement par l'emetteur. Si

vous n'etes pas destinataire de ce message, merci d'en avertir

immediatement l'expediteur.



This message is confidential ; its contents do not constitute a

commitment by Dexia Credit Local or Dexia CLF Banque. Any

unauthorised disclosure, use or dissemination, either whole or

partial, is prohibited. If you are not the intended recipient of

the message, please notify the sender immediately.

-----------------------------------------------------------------

Consultez notre site internet www.dexia-clf.fr

La cle d'une gestion optimisee du secteur public local

-----------------------------------------------------------------


More information about the Python-list mailing list