Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are Python function attributes?
In Python, everything is an object, and functions are no exception. Since functions are objects, they can have attributes ? custom data attached to them, just like attributes on a class instance. This allows you to store metadata, counters, configuration, or any arbitrary data directly on a function.
Function attributes are different from function parameters. Parameters are inputs passed during a function call, while attributes are persistent data stored on the function object itself.
Syntax
You can set and access function attributes using the dot (.) operator, or using setattr() and getattr() built-in functions −
# Using dot operator function_name.attribute = value print(function_name.attribute) # Using setattr / getattr setattr(function_name, 'attribute', value) print(getattr(function_name, 'attribute'))
To view all custom attributes on a function, use function_name.__dict__, which returns a dictionary of all user-defined attributes and their values.
Setting Attributes Inside a Function
In the below program, we define an attribute directly inside the function body, and set another attribute externally after calling the function −
Example
def function(): function.a = 14 print(function.a) function() function.name = "bar" print(function.__dict__)
The output of the above code is −
14
{'a': 14, 'name': 'bar'}
The attribute a is set inside the function when it runs, while name is set externally. Both appear in the function's __dict__.
Using setattr() and getattr()
In the below program, we define an empty function and then add attributes using setattr(). We retrieve them using both getattr() and the dot operator −
Example
def city(): pass setattr(city, 'Name', 'Hyderabad') setattr(city, 'State', 'Telangana') print(getattr(city, 'Name')) print(city.Name) print(city.State)
The output of the above code is −
Hyderabad Hyderabad Telangana
setattr() and getattr() are useful when attribute names are dynamic (stored in variables) rather than hardcoded.
Mixing Attributes with Dot Operator and setattr()
Example
def foo(): pass setattr(foo, 'age', 23) setattr(foo, 'name', 'John Doe') foo.gender = 'male' print(getattr(foo, 'age')) print(foo.gender) print(foo.name) print(foo.__dict__)
The output of the above code is −
23
male
John Doe
{'age': 23, 'name': 'John Doe', 'gender': 'male'}
Both methods ? dot operator and setattr() ? store attributes in the same __dict__ dictionary on the function object.
Built-in Function Attributes
Every Python function comes with several built-in attributes by default −
| Attribute | Description |
|---|---|
__name__ |
Name of the function |
__doc__ |
Docstring of the function |
__dict__ |
Dictionary of custom attributes |
__module__ |
Module where the function is defined |
__defaults__ |
Tuple of default parameter values |
__code__ |
Compiled bytecode of the function body |
Example
def greet(name="World"):
"""A simple greeting function"""
return f"Hello, {name}!"
print("Name:", greet.__name__)
print("Docstring:", greet.__doc__)
print("Defaults:", greet.__defaults__)
print("Module:", greet.__module__)
The output of the above code is −
Name: greet
Docstring: A simple greeting function
Defaults: ('World',)
Module: __main__
Conclusion
Python functions are objects and can carry custom attributes set via the dot operator or setattr(). These attributes persist on the function object and can be retrieved using dot access, getattr(), or __dict__. Every function also has built-in attributes like __name__, __doc__, and __defaults__ that provide metadata about the function itself.
