Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Python getAttr [] với sự trợ giúp của các ví dụ.
Phương thức
getattr[object, name[, default]]9 trả về giá trị của thuộc tính được đặt tên của một đối tượng. Nếu không tìm thấy, nó trả về giá trị mặc định được cung cấp cho hàm.
Thí dụ
class Student:
marks = 88
name = 'Sheeran'
person = Student[]
name = getattr[person, 'name']
print[name]
marks = getattr[person, 'marks']
print[marks]
# Output: Sheeran
# 88
Cú pháp GetAttr []
Cú pháp của phương pháp
getattr[object, name[, default]]9 là:
getattr[object, name[, default]]
Cú pháp trên tương đương với:
object.name
tham số getAttr []
Phương thức
getattr[object, name[, default]]9 mất nhiều tham số:
- Đối tượng - đối tượng có giá trị của thuộc tính được đặt tên - object whose named attribute's value is to be returned
- Tên - Chuỗi chứa tên của thuộc tính - string that contains the attribute's name
- mặc định [tùy chọn] - giá trị được trả về khi không tìm thấy thuộc tính được đặt tên - value that is returned when the named attribute is not found
getAttr [] giá trị trả về
Phương thức
getattr[object, name[, default]]9 Trả về:
- Giá trị của thuộc tính được đặt tên của đối tượng đã cho
object.name
3, nếu không tìm thấy thuộc tính có tênobject.name
4 Ngoại lệ, nếu không tìm thấy thuộc tính được đặt tên vàobject.name
3 không được xác định
Ví dụ 1: Làm thế nào GetAttr [] hoạt động trong Python?
class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
Đầu ra
The age is: 23 The age is: 23
Ví dụ 2: GetAttr [] khi không tìm thấy thuộc tính được đặt tên
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
Đầu ra
The sex is: Male AttributeError: 'Person' object has no attribute 'sex'
Ví dụ 2: GetAttr [] khi không tìm thấy thuộc tính được đặt tên
Các thuộc tính được đặt tên tình dục không có trong người lớp. Vì vậy, khi gọi phương thức
getattr[object, name[, default]]9 với giá trị mặc định
object.name7, nó sẽ trả về nam.
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1getattr[object, name[, default]]27
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 getattr[object, name[, default]]29____130
getattr[object, name[, default]]31
getattr[object, name[, default]]32
getattr[object, name[, default]]33
The age is: 23 The age is: 231
getattr[object, name[, default]]16
0class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
1class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
2class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
3class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
getattr[object, name[, default]]
40
0class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
1class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
getattr[object, name[, default]]
43
3class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
getattr[object, name[, default]]
29____130getattr[object, name[, default]]
31getattr[object, name[, default]]
48- Ví dụ 2: GetAttr [] khi không tìm thấy thuộc tính được đặt tên
- & NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số
getattr[object, name[, default]]
33The age is: 23 The age is: 23
1getattr[object, name[, default]]
16
0class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
1class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
3if __name__=='__main__': p = Person[] print[rgetattr[p, 'pet.favorite.color', 'calico']] # 'calico' try: # Without a default argument, `rgetattr`, like `getattr`, raises # AttributeError when the dotted attribute is missing print[rgetattr[p, 'pet.favorite.color']] except AttributeError as err: print[err] # 'Pet' object has no attribute 'favorite' rsetattr[p, 'pet.name', 'Sparky'] rsetattr[p, 'residence.type', 'Apartment'] print[p.__dict__] print[p.pet.name] # Sparky print[p.residence.type] # Apartment
3class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
2class Person: age = 23 name = "Adam" person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
5class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
7if __name__=='__main__': p = Person[] print[rgetattr[p, 'pet.favorite.color', 'calico']] # 'calico' try: # Without a default argument, `rgetattr`, like `getattr`, raises # AttributeError when the dotted attribute is missing print[rgetattr[p, 'pet.favorite.color']] except AttributeError as err: print[err] # 'Pet' object has no attribute 'favorite' rsetattr[p, 'pet.name', 'Sparky'] rsetattr[p, 'residence.type', 'Apartment'] print[p.__dict__] print[p.pet.name] # Sparky print[p.residence.type] # Apartment
7class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
import functools
def rsetattr[obj, attr, val]:
pre, _, post = attr.rpartition['.']
return setattr[rgetattr[obj, pre] if pre else obj, post, val]
# using wonder's beautiful simplification: //stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427
def rgetattr[obj, attr, *args]:
def _getattr[obj, attr]:
return getattr[obj, attr, *args]
return functools.reduce[_getattr, [obj] + attr.split['.']]
& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số
import functools
class Person[object]:
def __init__[self]:
self.pet = Pet[]
self.residence = Residence[]
class Pet[object]:
def __init__[self,name='Fido',species='Dog']:
self.name = name
self.species = species
class Residence[object]:
def __init__[self,type='House',sqft=None]:
self.type = type
self.sqft=sqft
def rsetattr[obj, attr, val]:
pre, _, post = attr.rpartition['.']
return setattr[rgetattr[obj, pre] if pre else obj, post, val]
def rgetattr[obj, attr, *args]:
def _getattr[obj, attr]:
return getattr[obj, attr, *args]
return functools.reduce[_getattr, [obj] + attr.split['.']]
if __name__=='__main__':
p = Person[]
print[rgetattr[p, 'pet.favorite.color', 'calico']]
# 'calico'
try:
# Without a default argument, `rgetattr`, like `getattr`, raises
# AttributeError when the dotted attribute is missing
print[rgetattr[p, 'pet.favorite.color']]
except AttributeError as err:
print[err]
# 'Pet' object has no attribute 'favorite'
rsetattr[p, 'pet.name', 'Sparky']
rsetattr[p, 'residence.type', 'Apartment']
print[p.__dict__]
print[p.pet.name]
# Sparky
print[p.residence.type]
# Apartment
if __name__=='__main__':
p = Person[]
print[rgetattr[p, 'pet.favorite.color', 'calico']]
# 'calico'
try:
# Without a default argument, `rgetattr`, like `getattr`, raises
# AttributeError when the dotted attribute is missing
print[rgetattr[p, 'pet.favorite.color']]
except AttributeError as err:
print[err]
# 'Pet' object has no attribute 'favorite'
rsetattr[p, 'pet.name', 'Sparky']
rsetattr[p, 'residence.type', 'Apartment']
print[p.__dict__]
print[p.pet.name]
# Sparky
print[p.residence.type]
# Apartment
9 getattr[object, name[, default]]00
getattr[object, name[, default]]33
The age is: 23 The age is: 231
getattr[object, name[, default]]16
0class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
1class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
2class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
3class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
getattr[object, name[, default]]
40
0class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
1class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
getattr[object, name[, default]]
43
3class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
getattr[object, name[, default]]
29____130getattr[object, name[, default]]
31getattr[object, name[, default]]
48- Ví dụ 2: GetAttr [] khi không tìm thấy thuộc tính được đặt tên
- & NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số
getattr[object, name[, default]]
33The age is: 23 The age is: 23
1getattr[object, name[, default]]
16
0class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
1class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
3if __name__=='__main__': p = Person[] print[rgetattr[p, 'pet.favorite.color', 'calico']] # 'calico' try: # Without a default argument, `rgetattr`, like `getattr`, raises # AttributeError when the dotted attribute is missing print[rgetattr[p, 'pet.favorite.color']] except AttributeError as err: print[err] # 'Pet' object has no attribute 'favorite' rsetattr[p, 'pet.name', 'Sparky'] rsetattr[p, 'residence.type', 'Apartment'] print[p.__dict__] print[p.pet.name] # Sparky print[p.residence.type] # Apartment
3class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
2class Person: age = 23 name = "Adam" person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
5class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
7if __name__=='__main__': p = Person[] print[rgetattr[p, 'pet.favorite.color', 'calico']] # 'calico' try: # Without a default argument, `rgetattr`, like `getattr`, raises # AttributeError when the dotted attribute is missing print[rgetattr[p, 'pet.favorite.color']] except AttributeError as err: print[err] # 'Pet' object has no attribute 'favorite' rsetattr[p, 'pet.name', 'Sparky'] rsetattr[p, 'residence.type', 'Apartment'] print[p.__dict__] print[p.pet.name] # Sparky print[p.residence.type] # Apartment
7class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
getattr[object, name[, default]]0
& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số
getattr[object, name[, default]]1
if __name__=='__main__':
p = Person[]
print[rgetattr[p, 'pet.favorite.color', 'calico']]
# 'calico'
try:
# Without a default argument, `rgetattr`, like `getattr`, raises
# AttributeError when the dotted attribute is missing
print[rgetattr[p, 'pet.favorite.color']]
except AttributeError as err:
print[err]
# 'Pet' object has no attribute 'favorite'
rsetattr[p, 'pet.name', 'Sparky']
rsetattr[p, 'residence.type', 'Apartment']
print[p.__dict__]
print[p.pet.name]
# Sparky
print[p.residence.type]
# Apartment
9 getattr[object, name[, default]]00
getattr[object, name[, default]]14
The age is: 23 The age is: 231
getattr[object, name[, default]]16
getattr[object, name[, default]]2
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
2 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
6class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1getattr[object, name[, default]]27
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 getattr[object, name[, default]]29____130
getattr[object, name[, default]]31
getattr[object, name[, default]]32
getattr[object, name[, default]]
33The age is: 23 The age is: 23
1getattr[object, name[, default]]
16
0class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
1class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
2class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
3class Person: age = 23 name = "Adam" person = Person[] # when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is providedprint['The sex is:', getattr[person, 'sex']]
getattr[object, name[, default]]
40
print['The sex is:', getattr[person, 'sex', 'Male']] print['The sex is:', getattr[person, 'sex']] print['The sex is:', getattr[person, 'sex', 'Male']] print['The sex is:', getattr[person, 'sex']] print['The sex is:', getattr[person, 'sex', 'Male']] print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
1class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
getattr[object, name[, default]]
43
3 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
getattr[object, name[, default]]
29____130getattr[object, name[, default]]
31 getattr[object, name[, default]]
48 function is used to access the attribute value of an object and also gives an option of executing the default value in case of unavailability of the key.
Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python getattr[obj, key, def]
Ví dụ 5: Chức năng Python getAttr []
9class Person: age = 23 name = "Adam" person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]getattr[object, name[, default]]73getattr[object, name[, default]]74getattr[object, name[, default]]75getattr[object, name[, default]]76The object whose attributes need to be processed. getattr[object, name[, default]]77getattr[object, name[, default]]75getattr[object, name[, default]]79The age is: 23 The age is: 231getattr[object, name[, default]]81The attribute of object getattr[object, name[, default]]77getattr[object, name[, default]]75getattr[object, name[, default]]84The age is: 23 The age is: 231getattr[object, name[, default]]86The default value that need to be printed in case attribute is not found.
9class Person: age = 23 name = "Adam" person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]getattr[object, name[, default]]73getattr[object, name[, default]]89getattr[object, name[, default]]75getattr[object, name[, default]]91Object value if value is available, default value in case attribute is not present
and returns AttributeError in case attribute is not present and default value is not
specified.
Cách thức hoạt động của GetAttr []
Ví dụ 1: Thể hiện hoạt động của getAttr [] & nbsp;
Python3
class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
7 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
8class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
9The age is: 23 The age is: 230
The age is: 23 The age is: 231
The age is: 23 The age is: 232
class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
9The age is: 23 The age is: 234
The age is: 23 The age is: 231
The age is: 23 The age is: 236
The age is: 23 The age is: 237
The age is: 23 The age is: 231
The age is: 23 The age is: 239
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
2 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
6class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1The sex is: Male AttributeError: 'Person' object has no attribute 'sex'0
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2The sex is: Male AttributeError: 'Person' object has no attribute 'sex'3
The sex is: Male AttributeError: 'Person' object has no attribute 'sex'4
The sex is: Male AttributeError: 'Person' object has no attribute 'sex'5
The sex is: Male AttributeError: 'Person' object has no attribute 'sex'6
The sex is: Male AttributeError: 'Person' object has no attribute 'sex'4
The sex is: Male AttributeError: 'Person' object has no attribute 'sex'8
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1import functools
def rsetattr[obj, attr, val]:
pre, _, post = attr.rpartition['.']
return setattr[rgetattr[obj, pre] if pre else obj, post, val]
# using wonder's beautiful simplification: //stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427
def rgetattr[obj, attr, *args]:
def _getattr[obj, attr]:
return getattr[obj, attr, *args]
return functools.reduce[_getattr, [obj] + attr.split['.']]
2 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5import functools
def rsetattr[obj, attr, val]:
pre, _, post = attr.rpartition['.']
return setattr[rgetattr[obj, pre] if pre else obj, post, val]
# using wonder's beautiful simplification: //stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427
def rgetattr[obj, attr, *args]:
def _getattr[obj, attr]:
return getattr[obj, attr, *args]
return functools.reduce[_getattr, [obj] + attr.split['.']]
6class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7Output:
getattr[object, name[, default]]3
Exception:
getattr[object, name[, default]]4
Ví dụ 2: GetAttr [] khi không tìm thấy thuộc tính được đặt tên
Python3
class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
7 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
8class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
9The age is: 23 The age is: 230
The age is: 23 The age is: 231
The age is: 23 The age is: 232
class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
9The age is: 23 The age is: 234
The age is: 23 The age is: 231
The age is: 23 The age is: 236
The age is: 23 The age is: 237
The age is: 23 The age is: 231
The age is: 23 The age is: 239
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
2 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
6class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7Output:
getattr[object, name[, default]]5
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
0 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
3
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
Python3
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1import functools
def rsetattr[obj, attr, val]:
pre, _, post = attr.rpartition['.']
return setattr[rgetattr[obj, pre] if pre else obj, post, val]
# using wonder's beautiful simplification: //stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427
def rgetattr[obj, attr, *args]:
def _getattr[obj, attr]:
return getattr[obj, attr, *args]
return functools.reduce[_getattr, [obj] + attr.split['.']]
2 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5import functools
def rsetattr[obj, attr, val]:
pre, _, post = attr.rpartition['.']
return setattr[rgetattr[obj, pre] if pre else obj, post, val]
# using wonder's beautiful simplification: //stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427
def rgetattr[obj, attr, *args]:
def _getattr[obj, attr]:
return getattr[obj, attr, *args]
return functools.reduce[_getattr, [obj] + attr.split['.']]
6class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
7 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
8class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
9The age is: 23 The age is: 230
The age is: 23 The age is: 231
The age is: 23 The age is: 232
class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
9The age is: 23 The age is: 234
The age is: 23 The age is: 231
The age is: 23 The age is: 236
The age is: 23 The age is: 237
The age is: 23 The age is: 231
The age is: 23 The age is: 239
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
2 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
6class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
2 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
6class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1The sex is: Male AttributeError: 'Person' object has no attribute 'sex'0
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2The sex is: Male AttributeError: 'Person' object has no attribute 'sex'3
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1import functools
def rsetattr[obj, attr, val]:
pre, _, post = attr.rpartition['.']
return setattr[rgetattr[obj, pre] if pre else obj, post, val]
# using wonder's beautiful simplification: //stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427
def rgetattr[obj, attr, *args]:
def _getattr[obj, attr]:
return getattr[obj, attr, *args]
return functools.reduce[_getattr, [obj] + attr.split['.']]
2 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5import functools
def rsetattr[obj, attr, val]:
pre, _, post = attr.rpartition['.']
return setattr[rgetattr[obj, pre] if pre else obj, post, val]
# using wonder's beautiful simplification: //stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427
def rgetattr[obj, attr, *args]:
def _getattr[obj, attr]:
return getattr[obj, attr, *args]
return functools.reduce[_getattr, [obj] + attr.split['.']]
6class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7Ví dụ 2: GetAttr [] khi không tìm thấy thuộc tính được đặt tên
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1if __name__=='__main__':
p = Person[]
print[rgetattr[p, 'pet.favorite.color', 'calico']]
# 'calico'
try:
# Without a default argument, `rgetattr`, like `getattr`, raises
# AttributeError when the dotted attribute is missing
print[rgetattr[p, 'pet.favorite.color']]
except AttributeError as err:
print[err]
# 'Pet' object has no attribute 'favorite'
rsetattr[p, 'pet.name', 'Sparky']
rsetattr[p, 'residence.type', 'Apartment']
print[p.__dict__]
print[p.pet.name]
# Sparky
print[p.residence.type]
# Apartment
3 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5if __name__=='__main__':
p = Person[]
print[rgetattr[p, 'pet.favorite.color', 'calico']]
# 'calico'
try:
# Without a default argument, `rgetattr`, like `getattr`, raises
# AttributeError when the dotted attribute is missing
print[rgetattr[p, 'pet.favorite.color']]
except AttributeError as err:
print[err]
# 'Pet' object has no attribute 'favorite'
rsetattr[p, 'pet.name', 'Sparky']
rsetattr[p, 'residence.type', 'Apartment']
print[p.__dict__]
print[p.pet.name]
# Sparky
print[p.residence.type]
# Apartment
7class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7Output:
getattr[object, name[, default]]6
& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số
Python3
class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
7 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
8class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
9The age is: 23 The age is: 230
The age is: 23 The age is: 231
The age is: 23 The age is: 232
class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
9The age is: 23 The age is: 234
The age is: 23 The age is: 231
The age is: 23 The age is: 236
The age is: 23 The age is: 237
The age is: 23 The age is: 231
The age is: 23 The age is: 239
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1import functools
def rsetattr[obj, attr, val]:
pre, _, post = attr.rpartition['.']
return setattr[rgetattr[obj, pre] if pre else obj, post, val]
# using wonder's beautiful simplification: //stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427
def rgetattr[obj, attr, *args]:
def _getattr[obj, attr]:
return getattr[obj, attr, *args]
return functools.reduce[_getattr, [obj] + attr.split['.']]
2 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5import functools
def rsetattr[obj, attr, val]:
pre, _, post = attr.rpartition['.']
return setattr[rgetattr[obj, pre] if pre else obj, post, val]
# using wonder's beautiful simplification: //stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427
def rgetattr[obj, attr, *args]:
def _getattr[obj, attr]:
return getattr[obj, attr, *args]
return functools.reduce[_getattr, [obj] + attr.split['.']]
6class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7Output:
getattr[object, name[, default]]7
Ví dụ 2: GetAttr [] khi không tìm thấy thuộc tính được đặt tên
Python3
class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
7 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
8class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1if __name__=='__main__':
p = Person[]
print[rgetattr[p, 'pet.favorite.color', 'calico']]
# 'calico'
try:
# Without a default argument, `rgetattr`, like `getattr`, raises
# AttributeError when the dotted attribute is missing
print[rgetattr[p, 'pet.favorite.color']]
except AttributeError as err:
print[err]
# 'Pet' object has no attribute 'favorite'
rsetattr[p, 'pet.name', 'Sparky']
rsetattr[p, 'residence.type', 'Apartment']
print[p.__dict__]
print[p.pet.name]
# Sparky
print[p.residence.type]
# Apartment
3 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
5if __name__=='__main__':
p = Person[]
print[rgetattr[p, 'pet.favorite.color', 'calico']]
# 'calico'
try:
# Without a default argument, `rgetattr`, like `getattr`, raises
# AttributeError when the dotted attribute is missing
print[rgetattr[p, 'pet.favorite.color']]
except AttributeError as err:
print[err]
# 'Pet' object has no attribute 'favorite'
rsetattr[p, 'pet.name', 'Sparky']
rsetattr[p, 'residence.type', 'Apartment']
print[p.__dict__]
print[p.pet.name]
# Sparky
print[p.residence.type]
# Apartment
7class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số
if __name__=='__main__':
p = Person[]
print[rgetattr[p, 'pet.favorite.color', 'calico']]
# 'calico'
try:
# Without a default argument, `rgetattr`, like `getattr`, raises
# AttributeError when the dotted attribute is missing
print[rgetattr[p, 'pet.favorite.color']]
except AttributeError as err:
print[err]
# 'Pet' object has no attribute 'favorite'
rsetattr[p, 'pet.name', 'Sparky']
rsetattr[p, 'residence.type', 'Apartment']
print[p.__dict__]
print[p.pet.name]
# Sparky
print[p.residence.type]
# Apartment
9 getattr[object, name[, default]]00
getattr[object, name[, default]]14
The age is: 23 The age is: 231
getattr[object, name[, default]]16
getattr[object, name[, default]]77
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0getattr[object, name[, default]]94
getattr[object, name[, default]]95
getattr[object, name[, default]]96
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1getattr[object, name[, default]]27
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
3 getattr[object, name[, default]]29____130
getattr[object, name[, default]]31
getattr[object, name[, default]]32
getattr[object, name[, default]]33
The age is: 23 The age is: 231
getattr[object, name[, default]]16
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0object.name07
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0object.name09
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
1class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2The sex is: Male AttributeError: 'Person' object has no attribute 'sex'3
object.name14
class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
print['The sex is:', getattr[person, 'sex', 'Male']]
# when no default value is provided
print['The sex is:', getattr[person, 'sex']]
7class Person:
age = 23
name = "Adam"
person = Person[]
print['The age is:', getattr[person, "age"]]
print['The age is:', person.age]
2The sex is: Male AttributeError: 'Person' object has no attribute 'sex'3
object.name14
object.name19
object.name20
getattr[object, name[, default]]96
Output:
getattr[object, name[, default]]8
print['The sex is:', getattr[person, 'sex', 'Male']] print['The sex is:', getattr[person, 'sex']] print['The sex is:', getattr[person, 'sex', 'Male']] print['The sex is:', getattr[person, 'sex']] print['The sex is:', getattr[person, 'sex', 'Male']] print['The sex is:', getattr[person, 'sex']] print['The sex is:', getattr[person, 'sex', 'Male']] print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
1class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
2 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
3 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
getattr[object, name[, default]]
40Conventional method takes less time than getattr[], but when default values have to be used in case of missing attributes, getattr[] is a good choice.
print['The sex is:', getattr[person, 'sex', 'Male']] print['The sex is:', getattr[person, 'sex']] print['The sex is:', getattr[person, 'sex', 'Male']] print['The sex is:', getattr[person, 'sex']] print['The sex is:', getattr[person, 'sex', 'Male']] print['The sex is:', getattr[person, 'sex']]
0class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
1class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
getattr[object, name[, default]]
43
3 class Person:
age = 23
name = "Adam"
person = Person[]
# when default value is provided
getattr[object, name[, default]]
29____130getattr[object, name[, default]]
31 getattr[object, name[, default]]
48The are many
applications of getattr[], a few of them already mentioned in cases of absence of attributes of objects, in web developments where some of the form attributes are optional. Also useful in cases of Machine Learning feature collections in case some features sometimes go missing in data collection.