2

How to check attr existence in function or method with hasattr (or without)? When I try to check it is False in any way:

>>> def f():
        at = True


>>> hasattr(f, 'at')
False
>>> hasattr(f(), 'at')
False
jamylak
  • 120,885
  • 29
  • 225
  • 225
I159
  • 27,484
  • 29
  • 93
  • 128

2 Answers2

5

Local variables are not attributes. You cannot use any *attr() to frob them.

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
  • +1. @I159: if you *really* want to see what local variables a function is using, [this code](http://stackoverflow.com/questions/1360721/how-to-get-set-local-variables-of-a-function-from-outside-in-python) is out there, but what on earth would be your use case? – David Robinson Apr 13 '12 at 05:25
0

It should work, look at below example.

>>> def f():
...    f.at = True
...
>>> hasattr(f, 'at')
False
>>> f()
>>> hasattr(f, 'at')
True