Bagaimana Anda memeriksa apakah self memiliki atribut python?

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# declaring class4# declaring class5# declaring class6# declaring class7

 

# 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# declaring class4# declaring class5class7# declaring class7

Keluaran.  

Does name exist ? True Does motto exist ? False

Contoh 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

________43______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-06

Hasil. 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

Bagaimana cara melihat atribut diri dengan Python?

Atribut kelas juga dapat diakses menggunakan metode dan fungsi bawaan berikut. .
getattr() – Fungsi ini digunakan untuk mengakses atribut objek
hasattr() – Fungsi ini digunakan untuk memeriksa apakah atribut ada atau tidak
setattr() – Fungsi ini digunakan untuk mengatur atribut

Bagaimana Anda memeriksa apakah ada atribut?

Untuk memeriksa apakah ada atribut dengan nama tertentu, Anda menggunakan metode hasAttribute(). .
hasil const = elemen. hasAttribute(nama);.
tautan const = dokumen. querySelector('a'); . hasAttribute('judul');.
tautan const = dokumen. querySelector('a'); . hasAttribute('data-method');

Bagaimana Anda memeriksa apakah suatu objek tidak memiliki atribut Python?

Untuk memeriksa apakah suatu objek memiliki atribut di Python, Anda dapat menggunakan fungsi hasattr() . Fungsi ini membutuhkan dua argumen. objek yang ingin Anda periksa, dan nama atribut yang Anda cari. Dalam contoh ini, hasattr(obj, 'attr') akan mengembalikan True , karena obj memiliki atribut attr.

Bagaimana Anda menemukan atribut suatu fungsi di Python?

fungsi Python getattr() . Fungsi python getattr () digunakan untuk mendapatkan nilai atribut objek dan jika tidak ada atribut objek yang ditemukan, nilai default dikembalikan.

Postingan terbaru

LIHAT SEMUA