Showing posts with label function-program-in-python. Show all posts
Showing posts with label function-program-in-python. Show all posts

Wednesday, November 25, 2020

, ,

hasattr() function in python with example

 Python hasattr() function in python return the true if the object gives the name of attribute and false if didn't return the name.

Syntax for hasattr()

hasattr(object, name)

hasattr() in python called by the getattr() to check about the error


Parameters For hasattr() function in python

There are two parameters which is used for this function the first one is object whose name is going to be checked.

And the name is second parameter which give the name for searched

Return value

It gives boolean return value True and False

Program Example For hasattr() in Python

class Person:
    age = 22
    name = 'Adam stiffman'

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