hasattr with functions
-
31-05-2021 - |
Frage
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
Lösung
Local variables are not attributes. You cannot use any *attr()
to frob them.
Andere Tipps
It should work, look at below example.
>>> def f():
... f.at = True
...
>>> hasattr(f, 'at')
False
>>> f()
>>> hasattr(f, 'at')
True
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow