The function hasattr () in Python
The hasattr () function in Python is used to check if the input object has the property you want to find.
The hasattr () function in Python is used to check if the input object has the property you want to find. How is the syntax of hasattr () function, what parameters does it have and how to use it? Invites you to read the track.
The syntax of hasattr () function in Python
hasattr(doituong, tenthuoctinh)
Parameters of hasattr () function :
The hasattr () function has 2 parameters:
- Doituong: object you want to check
- tenthuoctinh: name of the attribute to search
Note :
Function hasattr () returns:
- TRUE if the object has a transmission attribute.
- FALSE if the object has no transmission attribute.
Example: How does hasattr () function work?
class Nhanvien:
tuoi = 31
ten = 'QuachTinh'
nhanvien = Nhanvien()
print('Doi tuong Nhanvien co thuoc tinh tuoi khong?:', hasattr(nhanvien, 'tuoi'))
print('Doi tuong Nhanvien co thuoc tinh ten khong?:', hasattr(nhanvien, 'ten'))
print('Doi tuong Nhanvien co thuoc tinh luong khong?:', hasattr(nhanvien, 'luong'))
Run the program, the result is:
Doi tuong Nhanvien co thuoc tinh tuoi khong?: True
Doi tuong Nhanvien co thuoc tinh ten khong?: True
Doi tuong Nhanvien co thuoc tinh luong khong?: False
Previous lesson: Hex () function in Python
Next lesson: filter () function in Python
You've just finished reading the article "The function hasattr () in Python" edited by the TipsMake team. You can save the-function-hasattr-in-python.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.