vars().has_key() question about how working .

Paul McGuire ptmcg at austin.rr.com
Sun Apr 4 04:59:23 EDT 2010


On Apr 4, 3:42 am, "catalinf... at gmail.com" <catalinf... at gmail.com>
wrote:
> Hi everyone .
> My questions is "why vars().has_key('b') is False ?'
> I expecting to see "True" because is a variable ...
> Thanks

Yes, 'b' is a var, but only within the scope of something().  See how
this is different:

>>> def sth():
...   b = 25
...   print 'b' in vars()
...
>>> sth()
True

(Also, has_key() is the old-style way to test for key existence in a
dict, and is kept around for compatibility with old code, but the
preferred method now is to use 'in'.)

-- Paul



More information about the Python-list mailing list