- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Environmental Science
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are Python function attributes?
Everything in Python is an object, and almost everything has attributes and methods. In python, functions too are objects. So they have attributes like other objects. All functions have a built-in attribute __doc__, which returns the doc string defined in the function source code. We can also assign new attributes to them, as well as retrieve the values of those attributes.
For handling attributes, Python provides us with “getattr” and “setattr”, a function that takes three arguments. There is no difference between “setattr” and using the dot-notation on the left side of the = assignment operator:
The given code can be written as follows to assign and retrieve attributes.
Example
def foo(): pass setattr(foo, 'age', 23 ) setattr(foo, 'name', 'John Doe' ) print(getattr(foo, 'age')) foo.gender ='male' print(foo.gender) print(foo.name) print(foo.age)
Output
C:/Users/codegyan/~.py 23 male John Doe 23
- Related Questions & Answers
- What is SQL ?
- What does ‘<?=’ short open tag mean in PHP ?
- What are Python function attributes?
- How to Calculate Area of Circle using C Programming
- How to redirect HTTP to HTTPS Using htaccess
- How to Implement Stack Operations Using Array using C
- How to calculate sum of 5 subjects and find percentage Using C Program
- How to Calculate Area of Rectangle using C Program
- How To Clean Up Unnecessary Code From WordPress Header Without Plugins
- How To Send mail from your Gmail account using Python
- How To Reduce the Array to 0 by decreasing elements by 1 or replacing at most K elements by 0
- How to convert temperature from degree centigrade to Fahrenheit using C
- PHP Date and Time Function
- How can we create recursive functions in Python?
- Write a C program using pointers to read in an array of integers and print its elements in reverse order.
- Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers
Advertisements
ads