Python hasattr()

Configurare noua (How To)

Situatie

The hasattr() method returns true if an object has the given named attribute and false if it does not.

Solutie

Pasi de urmat

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

Tip solutie

Permanent

Voteaza

(6 din 12 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?