Function and Method Attributes
In Python 2.1, functions and methods can now have attributes
def foo(x):
...
foo.secure = 1
foo.private = 0
if foo.secure: foo(x)
- Attributes stored in underlying __dict__ attribute (like with classes)
Primary use
- Storing additional information about a function or method
- Useful in parser generators, network applications, etc.
Note: No way to initialize attributes in function definition
# This does not work
def foo(x):
foo.secure = 1
...
if foo.secure: foo(1) # AttributeError: secure not defined
|