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
  • 128,818
  • 30
  • 231
  • 230
I159
  • 29,741
  • 31
  • 97
  • 132

2 Answers2

5

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • +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