
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What does getattr() function do in Python?
Python getattr()
The getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.
Syntax
The syntax of getattr() method is −
getattr(object, name[, default])
The getattr() method can take multiple parameters −
The getattr() method returns −
value of the named attribute of the given object
default, if no named attribute is found
AttributeError exception, if named attribute is not found and default is not defined
Example
class Male: age = 21 name = "Abel" x = Male() print('The age is:', getattr(x, "age")) print('The age is:', x.age)
Output
This gives the output
('The age is:', 21) ('The age is:', 21)
- Related Articles
- What does reload() function do in Python?
- What does raw_input() function do in python?
- What does input() function do in python?
- What does print() function do in Python?
- What does open() function do in Python?
- What does close() function do in Python?
- What does os.pipe() function do in Python?
- What does hasattr() function do in Python?
- What does setattr() function do in Python?\n\n
- What does delattr() function do in Python?\n\n
- Python - getattr() method
- What does the repr() function do in Python Object Oriented Programming?
- What does the str() function do in Python Object Oriented Programming?
- What does the cmp() function do in Python Object Oriented Programming?
- What is difference in getattr() and setattr() functions in Python?

Advertisements