Hướng dẫn getattr() function python - hàm getattr () python
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ụ. Show
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ụ
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ố:
getAttr () giá trị trả vềPhương thức getattr(object, name[, default])9 Trả về:
Ví dụ 1: Làm thế nào GetAttr () hoạt động trong Python?
Đầ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
Đầ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. 0 1getattr(object, name[, default])27 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
& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số
9 getattr(object, name[, default])00 getattr(object, name[, default])33 The age is: 23 The age is: 231 getattr(object, name[, default])16
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 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 0 1 2 3 2 5 6 7 0 1getattr(object, name[, default])27 3 getattr(object, name[, default])29____130 getattr(object, name[, default])31 getattr(object, name[, default])32
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'))
Cách thức hoạt động của GetAttr ()Ví dụ 1: Thể hiện hoạt động của getAttr () & nbsp;Python3 7 8 9The age is: 23 The age is: 230 The age is: 23 The age is: 231 The age is: 23 The age is: 232 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 0 1 2 3 2 5 6 7 0 1The sex is: Male AttributeError: 'Person' object has no attribute 'sex'0 3 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 7 0 1 2 3 2 5 6 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ênPython3 7 8 9The age is: 23 The age is: 230 The age is: 23 The age is: 231 The age is: 23 The age is: 232 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 0 1 2 3 2 5 6 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'3Python3 0 1 2 3 2 5 6 7 7 8 9The age is: 23 The age is: 230 The age is: 23 The age is: 231 The age is: 23 The age is: 232 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 0 1 2 3 2 5 6 7 0 1 2 3 2 5 6 7 0 1The sex is: Male AttributeError: 'Person' object has no attribute 'sex'0 3 2The sex is: Male AttributeError: 'Person' object has no attribute 'sex'3 0 1 2 3 2 5 6 7Ví dụ 2: GetAttr () khi không tìm thấy thuộc tính được đặt tên 0 1 3 3 2 5 7 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 7 8 9The age is: 23 The age is: 230 The age is: 23 The age is: 231 The age is: 23 The age is: 232 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 0 1 2 3 2 5 6 7Output: getattr(object, name[, default])7 Ví dụ 2: GetAttr () khi không tìm thấy thuộc tính được đặt tênPython3 7 8 0 1 3 3 2 5 7 7& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số 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 0getattr(object, name[, default])94 getattr(object, name[, default])95 getattr(object, name[, default])96 0 1getattr(object, name[, default])27 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 0object.name07 0object.name09 0 1 2The sex is: Male AttributeError: 'Person' object has no attribute 'sex'3 object.name14 7 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')) 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')) |