
- 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 hasattr() function do in Python?
The hasattr() method in Python
The hasattr() method returns true if an object has the given named attribute and false if it does not.
Syntax
The syntax of hasattr() method is −
hasattr(object, name)
The hasattr() is called by getattr() to check to see if AttributeError is to be raised or not.
The hasattr() method takes two parameters −
The hasattr() method returns −
True, if object has the given named attribute
False, if object has no given named attribute
Example
class Male: age = 21 name = 'x' x = Male() print('Male has age?:', hasattr(x, 'age')) print('Male has salary?:', hasattr(x, 'salary'))
Output
This gives the output
('Male has age?:', True) ('Male has salary?:', False)
- 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 getattr() function do in Python?
- What does setattr() function do in Python?\n\n
- What does delattr() function do in Python?\n\n
- 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 does the .end() function do in jQuery?
- What does a +function() { } notation do in JavaScript?

Advertisements