[Tutor] python workspace -- vars() vs locals()

Emile van Sebille emile at fenx.com
Wed Jun 3 15:21:43 CEST 2009


On 6/3/2009 12:50 AM spir said...
> Le Tue, 02 Jun 2009 10:47:38 -0700,
> Emile van Sebille <emile at fenx.com> s'exprima ainsi:
> 
>> On 6/2/2009 8:54 AM roberto said...
>>> hello,
>>> i'd like to ask if there is anything in python which helps to see what
>>> variables have been defined and their type and their dimension etc;
>> In appropriate contexts, you may be able to use a variant of:
>>
>> from pprint import pprint
>> pprint (locals())
> 
> 
> By the way, it take the opportunity to ask about the difference between vars() & locals().

Well, start in the interpreter...
--------------
 >>> help (vars)
Help on built-in function vars in module __builtin__:

vars(...)
     vars([object]) -> dictionary

     Without arguments, equivalent to locals().
     With an argument, equivalent to object.__dict__.

 >>> help (locals)
Help on built-in function locals in module __builtin__:

locals(...)
     locals() -> dictionary

     Update and return a dictionary containing the current scope's local 
variables.
--------------

So, vars() is locals(), while vars(localvar) returns information about 
localvar.  In practice, you might do:
(taken from a zope project I did a few years back)

--------------registrant.py--------------
"""A Registrant type that has an editable schema"""

__author__  = 'Aaron VanDerlip aaron at netcorps.org'
__docformat__ = 'plaintext'

import pdb;pdb.set_trace()

from AccessControl import ClassSecurityInfo

--------------snip balance--------------

Then when run:
--------------
 > c:\...\registrant.py(8)?()
-> from AccessControl import ClassSecurityInfo

(Pdb) import pprint

(Pdb) pprint.pprint(vars())
{'__author__': 'Aaron VanDerlip aaron at netcorps.org',
  '__builtins__': <module '__builtin__' (built-in)>,
  '__doc__': 'A Registrant type that has an editable schema',
  '__docformat__': 'plaintext',
  '__file__': 'C:\\...\\registrant.py',
  '__name__': '__main__',
  'pdb': <module 'pdb' from 'C:\Python24\lib\pdb.pyc'>,
  'pprint': <module 'pprint' from 'C:\Python24\lib\pprint.pyc'>}

(Pdb) pprint.pprint(locals())
{'__author__': 'Aaron VanDerlip aaron at netcorps.org',
  '__builtins__': <module '__builtin__' (built-in)>,
  '__doc__': 'A Registrant type that has an editable schema',
  '__docformat__': 'plaintext',
  '__file__': 'C:\\...\\registrant.py',
  '__name__': '__main__',
  'pdb': <module 'pdb' from 'C:\Python24\lib\pdb.pyc'>,
  'pprint': <module 'pprint' from 'C:\Python24\lib\pprint.pyc'>}
--------------

So, vars() and local() are the same.  Now a look at vars(pdb)

--------------
(Pdb) pprint.pprint(vars(pdb))
{'Pdb': <class pdb.Pdb at 0x00FFAE40>,
  'Repr': <class repr.Repr at 0x00FFA900>,
  'TESTCMD': 'import x; x.main()',
  '__all__': ['run',
              'pm',
              'Pdb',
              'runeval',
              'runctx',
              'runcall',
              'set_trace',
              'post_mortem',
              'help'],

<snip balance>

Emile

> 
> Denis
> ------
> la vita e estrany
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list