
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 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)
Advertisements