Table of Contents
The hasattr() method returns true if an object has the given named attribute and false if it does not.
The syntax of hasattr()
method is:
hasattr(object, name)
hasattr()
is called by getattr() to check to see if AttributeError is to be raised or not.
1. hasattr() Parameters
hasattr()
method takes two parameters:
- object – object whose named attribute is to be checked
- name – name of the attribute to be searched
2. Return value from hasattr()
hasattr()
method returns:
- True, if object has the given named attribute
- False, if object has no given named attribute
3. Example: How hasattr() works in Python?
class Person: age = 23 name = 'Adam' person = Person() print('Person has age?:', hasattr(person, 'age')) print('Person has salary?:', hasattr(person, 'salary'))
Output
Person has age?: True Person has salary?: False
Related posts:
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Operators
Python String lstrip()
Python Anonymous / Lambda Function
Python iter()
Python round()
Python Namespace and Scope
Deep Learning in Python - LazyProgrammer
Python Program to Measure the Elapsed Time in Python
Python String join()
Python Program to Find HCF or GCD
Python Program to Copy a File
Python format()
Python Program to Find LCM
Python Program to Find the Factorial of a Number
Python Keywords and Identifiers
Python Set difference()
Python Program to Get File Creation and Modification Date
Python List remove()
Python del Statement
Python tuple()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python String translate()
Python Data Structures and Algorithms - Benjamin Baka
Python Program to Check If a List is Empty
Python String startswith()
Python Program to Merge Mails
Python property()
Python if...else Statement
Python map()
Python String split()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...