Fungsi python getattr() digunakan untuk mengakses nilai atribut suatu objek dan juga memberikan opsi untuk mengeksekusi nilai default jika kunci tidak tersedia
Sintaksis. getattr(obj, kunci, def)
Parameter.
- obj. Objek yang atributnya perlu diproses
- kunci. Atribut objek
- def. Nilai default yang perlu dicetak jika atribut case tidak ditemukan
Pengembalian. Nilai objek jika nilai tersedia, nilai default jika atribut tidak ada
dan mengembalikan AttributeError jika atribut tidak ada dan nilai default tidak ada
ditentukan.
Bagaimana getattr() bekerja dengan Python
Contoh 1. Mendemonstrasikan cara kerja getattr()
Python3
# Python code to demonstrate
# working of getattr()
# declaring class
class AttributeError: GfG instance has no attribute 'motto'0
AttributeError: GfG instance has no attribute 'motto'1AttributeError: GfG instance has no attribute 'motto'2AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'4
AttributeError: GfG instance has no attribute 'motto'1AttributeError: GfG instance has no attribute 'motto'6AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'8
AttributeError: GfG instance has no attribute 'motto'_9
AttributeError: 'GfG' object has no attribute 'gender'_0
AttributeError: 'GfG' object has no attribute 'gender'1AttributeError: GfG instance has no attribute 'motto'3 AttributeError: 'GfG' object has no attribute 'gender'3
AttributeError: 'GfG' object has no attribute 'gender'_4
AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: 'GfG' object has no attribute 'gender'6AttributeError: 'GfG' object has no attribute 'gender'7 AttributeError: 'GfG' object has no attribute 'gender'8 AttributeError: 'GfG' object has no attribute 'gender'9The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-060The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-061The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-062
The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-063
AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: 'GfG' object has no attribute 'gender'6The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-066 AttributeError: 'GfG' object has no attribute 'gender'8 AttributeError: 'GfG' object has no attribute 'gender'9The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-060
________30______0AttributeError: 'GfG' object has no attribute 'motto'1AttributeError: 'GfG' object has no attribute 'motto'2
________30______0AttributeError: 'GfG' object has no attribute 'motto'4The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-062
AttributeError: 'GfG' object has no attribute 'motto'6
AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: 'GfG' object has no attribute 'gender'6AttributeError: 'GfG' object has no attribute 'motto'9 AttributeError: 'GfG' object has no attribute 'gender'8 AttributeError: 'GfG' object has no attribute 'gender'9The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-060<__main__.GfG object at 0x0000023C1ED92748> <class '__main__.GfG'> <bound method GfG.call of <__main__.GfG object at 0x0000023C1ED92748>> Vivek called with parameters 'arg'3The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-062
Keluaran.
The name is GeeksforGeeks Description is CS PortalPengecualian.
AttributeError: GfG instance has no attribute 'motto'Contoh 2. getattr() ketika atribut bernama tidak ditemukan
Python3
# Python code to demonstrate
# working of getattr()
# declaring class
class AttributeError: GfG instance has no attribute 'motto'0
AttributeError: GfG instance has no attribute 'motto'1AttributeError: GfG instance has no attribute 'motto'2AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'4
AttributeError: GfG instance has no attribute 'motto'1AttributeError: GfG instance has no attribute 'motto'6AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'8
# Python code to demonstrate_8
AttributeError: 'GfG' object has no attribute 'gender'1AttributeError: GfG instance has no attribute 'motto'3 AttributeError: 'GfG' object has no attribute 'gender'3
AttributeError: 'GfG' object has no attribute 'motto'6
AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: 'GfG' object has no attribute 'gender'6# working of getattr()5 AttributeError: 'GfG' object has no attribute 'gender'8 AttributeError: 'GfG' object has no attribute 'gender'9The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-060# working of getattr()9The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-062
Keluaran
AttributeError: 'GfG' object has no attribute 'gender'Contoh 3. Analisis Kinerja dan getattr python dengan parameter
Python3
# Python code to demonstrate
# declaring class_2
# declaring class3 # declaring class4
# declaring class
class AttributeError: GfG instance has no attribute 'motto'0
AttributeError: GfG instance has no attribute 'motto'1AttributeError: GfG instance has no attribute 'motto'2AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'4
AttributeError: GfG instance has no attribute 'motto'1AttributeError: GfG instance has no attribute 'motto'6AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'8
# Python code to demonstrate_8
AttributeError: 'GfG' object has no attribute 'gender'1AttributeError: GfG instance has no attribute 'motto'3 AttributeError: 'GfG' object has no attribute 'gender'3
AttributeError: GfG instance has no attribute 'motto'_00
AttributeError: GfG instance has no attribute 'motto'01AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'03
AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: 'GfG' object has no attribute 'gender'6AttributeError: 'GfG' object has no attribute 'gender'7 AttributeError: 'GfG' object has no attribute 'gender'8 AttributeError: 'GfG' object has no attribute 'gender'9The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-060The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-061The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-062
AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: 'GfG' object has no attribute 'gender'6AttributeError: GfG instance has no attribute 'motto'14 AttributeError: 'GfG' object has no attribute 'gender'8 AttributeError: GfG instance has no attribute 'motto'16AttributeError: GfG instance has no attribute 'motto'17AttributeError: GfG instance has no attribute 'motto'18 AttributeError: GfG instance has no attribute 'motto'19
AttributeError: GfG instance has no attribute 'motto'_20
AttributeError: GfG instance has no attribute 'motto'21AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'03
AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: 'GfG' object has no attribute 'gender'6AttributeError: 'GfG' object has no attribute 'gender'7 AttributeError: 'GfG' object has no attribute 'gender'8 AttributeError: GfG instance has no attribute 'motto'28
AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: 'GfG' object has no attribute 'gender'6AttributeError: GfG instance has no attribute 'motto'31 AttributeError: 'GfG' object has no attribute 'gender'8 AttributeError: GfG instance has no attribute 'motto'16AttributeError: GfG instance has no attribute 'motto'17AttributeError: GfG instance has no attribute 'motto'18 AttributeError: GfG instance has no attribute 'motto'36
Keluaran.
The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-06Contoh 4. getattr nilai default Python
Python3
# Python code to demonstrate
# working of getattr()
# declaring class
class AttributeError: GfG instance has no attribute 'motto'0
AttributeError: GfG instance has no attribute 'motto'1AttributeError: GfG instance has no attribute 'motto'2AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'4
AttributeError: GfG instance has no attribute 'motto'1AttributeError: GfG instance has no attribute 'motto'6AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'8
# Python code to demonstrate_8
AttributeError: 'GfG' object has no attribute 'gender'1AttributeError: GfG instance has no attribute 'motto'3 AttributeError: 'GfG' object has no attribute 'gender'3
AttributeError: 'GfG' object has no attribute 'motto'6
AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: 'GfG' object has no attribute 'gender'6AttributeError: 'GfG' object has no attribute 'motto'9 AttributeError: 'GfG' object has no attribute 'gender'8 AttributeError: 'GfG' object has no attribute 'gender'9The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-060<__main__.GfG object at 0x0000023C1ED92748> <class '__main__.GfG'> <bound method GfG.call of <__main__.GfG object at 0x0000023C1ED92748>> Vivek called with parameters 'arg'3The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-062
Keluaran
AttributeError: 'GfG' object has no attribute 'motto'Contoh 5. Pemanggilan fungsi python getattr()
Python3
# Python code to demonstrate
# working of getattr()
# declaring class
class AttributeError: GfG instance has no attribute 'motto'0
AttributeError: GfG instance has no attribute 'motto'_1
AttributeError: GfG instance has no attribute 'motto'1AttributeError: GfG instance has no attribute 'motto'70 AttributeError: GfG instance has no attribute 'motto'71AttributeError: GfG instance has no attribute 'motto'72AttributeError: GfG instance has no attribute 'motto'73
AttributeError: GfG instance has no attribute 'motto'74AttributeError: GfG instance has no attribute 'motto'72AttributeError: GfG instance has no attribute 'motto'76AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'2
AttributeError: GfG instance has no attribute 'motto'74AttributeError: GfG instance has no attribute 'motto'72AttributeError: GfG instance has no attribute 'motto'81AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'6
AttributeError: GfG instance has no attribute 'motto'1AttributeError: GfG instance has no attribute 'motto'70 AttributeError: GfG instance has no attribute 'motto'86AttributeError: GfG instance has no attribute 'motto'72AttributeError: GfG instance has no attribute 'motto'88
AttributeError: GfG instance has no attribute 'motto'74AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: GfG instance has no attribute 'motto'91AttributeError: GfG instance has no attribute 'motto'92________0______93
AttributeError: GfG instance has no attribute 'motto'74AttributeError: GfG instance has no attribute 'motto'95
# Python code to demonstrate_8
AttributeError: 'GfG' object has no attribute 'gender'1AttributeError: GfG instance has no attribute 'motto'3 AttributeError: GfG instance has no attribute 'motto'99AttributeError: 'GfG' object has no attribute 'gender'00AttributeError: 'GfG' object has no attribute 'motto'2AttributeError: 'GfG' object has no attribute 'gender'02AttributeError: GfG instance has no attribute 'motto'93
________10AttributeError: GfG instance has no attribute 'motto'_10_______05
________10AttributeError: GfG instance has no attribute 'motto'_10_______07
AttributeError: 'GfG' object has no attribute 'gender'5AttributeError: 'GfG' object has no attribute 'gender'6AttributeError: 'GfG' object has no attribute 'gender'9The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-060AttributeError: 'GfG' object has no attribute 'gender'12The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-062
AttributeError: 'GfG' object has no attribute 'gender'9The name is GeeksforGeeks Time to execute getattr 5.0067901611328125e-06 The name is GeeksforGeeks Time to execute conventional method 1.1920928955078125e-060AttributeError: 'GfG' object has no attribute 'gender'12AttributeError: 'GfG' object has no attribute 'gender'17AttributeError: 'GfG' object has no attribute 'gender'18AttributeError: GfG instance has no attribute 'motto'93
Keluaran
<__main__.GfG object at 0x0000023C1ED92748> <class '__main__.GfG'> <bound method GfG.call of <__main__.GfG object at 0x0000023C1ED92748>> Vivek called with parameters 'arg'Hasil. Metode konvensional memakan waktu lebih sedikit daripada getattr(), tetapi ketika nilai default harus digunakan jika ada atribut yang hilang, getattr() adalah pilihan yang baik
Aplikasi. Ada banyak aplikasi getattr(), beberapa di antaranya telah disebutkan dalam kasus tidak adanya atribut objek, dalam pengembangan web di mana beberapa atribut bentuk bersifat opsional. Juga berguna dalam kasus pengumpulan fitur Pembelajaran Mesin jika beberapa fitur terkadang hilang dalam pengumpulan data