Fungsi isinstance() mengembalikan True jika objek yang ditentukan adalah jenis yang ditentukan, jika tidak False
Jika parameter tipe adalah tuple, fungsi ini akan mengembalikan True jika objek adalah salah satu tipe di tuple
Fungsi hasattr () Python adalah fungsi utilitas bawaan, yang digunakan untuk memeriksa apakah suatu objek memiliki atribut bernama yang diberikan dan mengembalikan true jika ada, jika tidak salah. Pada artikel ini, kita akan melihat cara memeriksa apakah suatu objek memiliki atribut dengan Python
Sintaks fungsi hasattr()
Sintaksis. hasattr(obj, kunci)
Parameter.
- keberatan. Objek yang mana atributnya harus diperiksa
- kunci. Atribut yang perlu diperiksa
Pengembalian. Mengembalikan True, jika atribut ada, yang lain mengembalikan False.
Contoh 1. Contoh fungsi python hasattr()
Di sini kita akan memeriksa apakah suatu objek memiliki atribut, untuk menemukan atribut objek di python kita telah menunjukkan kode berikut
Python3
# declaring class
class GfG:
name= "GeeksforGeeks"
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-060= No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-062
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-06_3
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-064= No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-066
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-06_7
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-068No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069# declaring class0 # declaring class1 # declaring class2No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069________41______4# declaring class5# declaring class6________41______7
# declaring class_8
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-068No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069class1 # declaring class1 # declaring class2No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069________41______4# declaring class5class7________41______7
Keluaran.
Does name exist ? True Does motto exist ? FalseContoh 2. Analisis performa antara metode hasattr() dan pernyataan try
Ini adalah Cara Sederhana untuk Memeriksa apakah suatu Objek memiliki Atribut dengan Python atau tidak menggunakan analisis kinerja antara fungsi hasattr() dan pernyataan try
Python3
class9 GfG:0
# declaring class
class GfG:
name= "GeeksforGeeks"
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-060= No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-062
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-06_3
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-064= No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-066
_6
7= 9
name0No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069# declaring class4# declaring class5class7name5
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-068No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069name9=0
=1=2
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-068No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069=6=0
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-068No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069"GeeksforGeeks"0 # declaring class1 # declaring class2"GeeksforGeeks"3"GeeksforGeeks"4 "GeeksforGeeks"5
"GeeksforGeeks"6
"GeeksforGeeks"7= 9
0=2
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-068 4
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-068No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069name9=0
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-0600 No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-0601
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-068No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069=6=0
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-068No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-069No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-0609 # declaring class1 # declaring class2"GeeksforGeeks"3"GeeksforGeeks"4 No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-0614
Keluaran.
No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-06Hasil. Mencoba/kecuali konvensional membutuhkan waktu lebih sedikit daripada hasattr(), tetapi untuk keterbacaan kode, hasattr() selalu merupakan pilihan yang lebih baik
Aplikasi. Fungsi ini dapat digunakan untuk memeriksa kunci guna menghindari kesalahan yang tidak perlu jika mengakses kunci yang tidak ada. Rantai hasattr() kadang-kadang digunakan untuk menghindari masuknya satu atribut terkait jika yang lain tidak ada