3 ms·
You can assign an attribute to a function in Python since functions have a __dict__. for example: def foo(): pass foo.a="asdf" >>> foo.a 'asdf' >>> foo.__d
by kansface 15y ago
You can assign an attribute to a function in Python since functions have a __dict__. for example:
def foo(): pass
foo.a="asdf"
>>> foo.a
'asdf'
>>> foo.__dict__
{'a': 'asdf'}
or alternatively:
>>> def bar():
... bar.a = "asdf"
...
>>> bar.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'a'
>>> bar()
>>> bar.a
'asdf'