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.
Table of Contents
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
5 ★ | 1 Vote
Read More
- Zip () function in Python
- Int () function in Python
- The function id () in Python
- The oct () function in Python
- The next () function in Python
- Hex () function in Python
- The map () function in Python
- The function dir () in Python
- The function set () in Python
- Help () function in Python
- Sum () function in Python
- The slice () function in Python
- The chr () function in Python
- The pow () function in Python