Xây dựng
getattr[myObj.contained, "c"]
7 không hoạt động. Các thực tiễn tốt nhất để thay thế công trình này là gì? Chia điều đó thành hai tuyên bố GetAttr?
Mazdak
102K18 Huy hiệu vàng157 Huy hiệu bạc183 Huy hiệu đồng18 gold badges157 silver badges183 bronze badges
Đã hỏi ngày 5 tháng 3 năm 2017 lúc 8:34Mar 5, 2017 at 8:34
1
Bạn có thể sử dụng
getattr[myObj.contained, "c"]
8 để có được nhiều thuộc tính cùng một lúc:from operator import attrgetter
my_attrs = attrgetter[attr1, attr2][obj]
Đã trả lời ngày 5 tháng 3 năm 2017 lúc 8:36Mar 5, 2017 at 8:36
MazdakmazdakMazdak
102K18 Huy hiệu vàng157 Huy hiệu bạc183 Huy hiệu đồng18 gold badges157 silver badges183 bronze badges
4
Đã hỏi ngày 5 tháng 3 năm 2017 lúc 8:34
Bạn có thể sử dụng
getattr[myObj.contained, "c"]
8 để có được nhiều thuộc tính cùng một lúc:def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
Đã trả lời ngày 5 tháng 3 năm 2017 lúc 8:36
MazdakmazdakSep 21, 2018 at 14:39
Như đã nêu trong câu trả lời này, giải pháp đơn giản nhất sẽ là sử dụng toán tử.Attrgetter [thêm thông tin trong trang Docs Python này].
my_attrs = [getattr[obj, attr] for attr in attr_list]
Nếu vì một lý do nào đó, giải pháp này không làm bạn hài lòng, bạn có thể sử dụng đoạn mã này:Jul 15, 2021 at 20:00
Từ trang này, hoạt động. Tôi đã thử nghiệm và sử dụng nó.Darren G
Đã trả lời ngày 21 tháng 9 năm 2018 lúc 14:397 silver badges15 bronze badges
Nếu bạn có tên thuộc tính bạn muốn có trong danh sách, bạn có thể thực hiện như sau:
aval, bval = getattr[myObj,"a"], getattr[myObj,"b"]
Đã trả lời ngày 15 tháng 7 năm 2021 lúc 20:00
getattr[myObj.contained, "c"]
Darren Gdarren g
6717 Huy hiệu bạc15 Huy hiệu ĐồngJul 15, 2021 at 20:22
Một cách đơn giản, nhưng không hùng hồn, để có được nhiều attr sẽ là sử dụng các bộ đếm có hoặc không có dấu ngoặc nhưTman
Nhưng tôi nghĩ rằng bạn có thể muốn thay vào đó để có được một đối tượng chứa một đối tượng với cách bạn đang sử dụng ký hiệu DOT. Trong trường hợp đó nó sẽ giống như2 bronze badges
Trong đó chứa là một đối tượng được chọn trong đối tượng MyOBJ và C là một thuộc tính của chứa. Hãy cho tôi biết nếu đây không phải là những gì bạn muốn.
from operator import attrgetter
attrgetter['attr0.attr1.attr2.attr3'][obj]
Đã trả lời ngày 15 tháng 7 năm 2021 lúc 20:22Jun 21 at 9:04
TmantmanVova
Phù hiệu bằng đồng 1122 gold badges11 silver badges18 bronze badges
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ố
0 và tương tự, dọc theo các dòng của:d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
4def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
81d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
4def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
83d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
4def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
5def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
8def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
07def multi_getattr[obj, attr, default = None]: """ Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is equivalent to x.a.b.c.d. When a default argument is given, it is returned when any attribute in the chain doesn't exist; without it, an exception is raised when a missing attribute is encountered. """ attributes = attr.split["."] for i in attributes: try: obj = getattr[obj, i] except AttributeError: if default: return default else: raise return obj # Example usage obj = [1,2,3] attr = "append.__doc__.capitalize.__doc__" multi_getattr[obj, attr] #Will return the docstring for the #capitalize method of the builtin string #object
88d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
01def multi_getattr[obj, attr, default = None]: """ Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is equivalent to x.a.b.c.d. When a default argument is given, it is returned when any attribute in the chain doesn't exist; without it, an exception is raised when a missing attribute is encountered. """ attributes = attr.split["."] for i in attributes: try: obj = getattr[obj, i] except AttributeError: if default: return default else: raise return obj # Example usage obj = [1,2,3] attr = "append.__doc__.capitalize.__doc__" multi_getattr[obj, attr] #Will return the docstring for the #capitalize method of the builtin string #object
8def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
07def multi_getattr[obj, attr, default = None]: """ Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is equivalent to x.a.b.c.d. When a default argument is given, it is returned when any attribute in the chain doesn't exist; without it, an exception is raised when a missing attribute is encountered. """ attributes = attr.split["."] for i in attributes: try: obj = getattr[obj, i] except AttributeError: if default: return default else: raise return obj # Example usage obj = [1,2,3] attr = "append.__doc__.capitalize.__doc__" multi_getattr[obj, attr] #Will return the docstring for the #capitalize method of the builtin string #object
88d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
93d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
94d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
70d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
- Việc đệ quy là khá rõ ràng tại sao nó xảy ra ... nhưng làm thế nào để tôi tránh nó?Conventional method takes less time than getattr[], but when default values have to be used in case of missing attributes, getattr[] is a good choice.
- 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ố
- Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python
- Ví dụ 5: Chức năng Python getAttr []
Những gì tôi muốn có thể làm là khi một ví dụ của cha mẹ đề cập đến các thuộc tính của nó, thay vào đó, nó gọi các phương thức của trẻ.
0 và tương tự, dọc theo các dòng của:d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
4def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
81d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
4def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
83d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
4def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
5def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
8def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
07def multi_getattr[obj, attr, default = None]: """ Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is equivalent to x.a.b.c.d. When a default argument is given, it is returned when any attribute in the chain doesn't exist; without it, an exception is raised when a missing attribute is encountered. """ attributes = attr.split["."] for i in attributes: try: obj = getattr[obj, i] except AttributeError: if default: return default else: raise return obj # Example usage obj = [1,2,3] attr = "append.__doc__.capitalize.__doc__" multi_getattr[obj, attr] #Will return the docstring for the #capitalize method of the builtin string #object
88d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
01def multi_getattr[obj, attr, default = None]: """ Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is equivalent to x.a.b.c.d. When a default argument is given, it is returned when any attribute in the chain doesn't exist; without it, an exception is raised when a missing attribute is encountered. """ attributes = attr.split["."] for i in attributes: try: obj = getattr[obj, i] except AttributeError: if default: return default else: raise return obj # Example usage obj = [1,2,3] attr = "append.__doc__.capitalize.__doc__" multi_getattr[obj, attr] #Will return the docstring for the #capitalize method of the builtin string #object
8def __getattr__[self, key]: if isinstance[getattr[self, key], Field]: return getattr[self, key].get[] return getattr[self, key]
07def multi_getattr[obj, attr, default = None]: """ Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is equivalent to x.a.b.c.d. When a default argument is given, it is returned when any attribute in the chain doesn't exist; without it, an exception is raised when a missing attribute is encountered. """ attributes = attr.split["."] for i in attributes: try: obj = getattr[obj, i] except AttributeError: if default: return default else: raise return obj # Example usage obj = [1,2,3] attr = "append.__doc__.capitalize.__doc__" multi_getattr[obj, attr] #Will return the docstring for the #capitalize method of the builtin string #object
88d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
93d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
94d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
70d = Document[] d.y = 'some value' # Calls the `set` function of the Field d.y == 'some value' # Calls the `get` function of the Field del d.y # Calls the `delete` function of the Field
- Việc đệ quy là khá rõ ràng tại sao nó xảy ra ... nhưng làm thế nào để tôi tránh nó?Conventional method takes less time than getattr[], but when default values have to be used in case of missing attributes, getattr[] is a good choice.
- 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ố
- Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python
- Ví dụ 5: Chức năng Python getAttr []
class Field:
def get[self]:
def set[self, value]:
def delete[self]:
class Document:
def __init__[self]:
self.y = Field[]
self.z = True
Những gì tôi muốn có thể làm là khi một ví dụ của cha mẹ đề cập đến các thuộc tính của nó, thay vào đó, nó gọi các phương thức của trẻ.
d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
Điều khác là tôi chỉ muốn hành vi này khi trường thuộc loại
getattr[myObj.contained, "c"]
9.Tôi đang gặp phải các vấn đề đệ quy cố gắng sử dụng
d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
0 và tương tự, dọc theo các dòng của:Việc đệ quy là khá rõ ràng tại sao nó xảy ra ... nhưng làm thế nào để tôi tránh nó?
Tôi đã thấy một vài ví dụ đã có trên StackOverflow, nhưng dường như tôi không thể tìm ra cách khắc phục nó.
- 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ố
0 và tương tự, dọc theo các dòng của:d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
4def __getattr__[self, key]:
if isinstance[getattr[self, key], Field]:
return getattr[self, key].get[]
return getattr[self, key]
81d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
4def __getattr__[self, key]:
if isinstance[getattr[self, key], Field]:
return getattr[self, key].get[]
return getattr[self, key]
83d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
4def __getattr__[self, key]:
if isinstance[getattr[self, key], Field]:
return getattr[self, key].get[]
return getattr[self, key]
5def __getattr__[self, key]:
if isinstance[getattr[self, key], Field]:
return getattr[self, key].get[]
return getattr[self, key]
8def __getattr__[self, key]:
if isinstance[getattr[self, key], Field]:
return getattr[self, key].get[]
return getattr[self, key]
07def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
88d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
01def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
8def __getattr__[self, key]:
if isinstance[getattr[self, key], Field]:
return getattr[self, key].get[]
return getattr[self, key]
07def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
88d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
93d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
94d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
70 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.d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
Việc đệ quy là khá rõ ràng tại sao nó xảy ra ... nhưng làm thế nào để tôi tránh nó?Conventional method takes less time than getattr[], but when default values have to be used in case of missing attributes, getattr[] is a good choice. getattr[obj, key, def]
Tôi đã thấy một vài ví dụ đã có trên StackOverflow, nhưng dường như tôi không thể tìm ra cách khắc phục nó.The 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.
- Tôi có một lớp học là một trường hợp của một lớp khác.The object whose attributes need to be processed.
- Nội dung chínhThe attribute of object
- Cách thức hoạt động của GetAttr []The default value that need to be printed in case attribute is not found.
Trả về: Giá trị đối tượng Nếu giá trị có sẵn, giá trị mặc định trong trường hợp thuộc tính không có mặt & nbsp; và returns AttributionError trong trường hợp thuộc tính không có mặt và giá trị mặc định là không & nbsp; được chỉ định. & NBSP;Object 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. Object 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
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ố
Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python
Ví dụ 5: Chức năng Python getAttr []
Những gì tôi muốn có thể làm là khi một ví dụ của cha mẹ đề cập đến các thuộc tính của nó, thay vào đó, nó gọi các phương thức của trẻ.
Điều khác là tôi chỉ muốn hành vi này khi trường thuộc loại
getattr[myObj.contained, "c"]
9.def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
08def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
09def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
20def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
08def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
22def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
01Tôi đang gặp phải các vấn đề đệ quy cố gắng sử dụng
d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
0 và tương tự, dọc theo các dòng của:Output:
def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
0Exception:
def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
2Ví dụ 2: GetAttr [] khi không tìm thấy thuộc tính được đặt tên
Python3
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ố
Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python
Ví dụ 5: Chức năng Python getAttr []
Những gì tôi muốn có thể làm là khi một ví dụ của cha mẹ đề cập đến các thuộc tính của nó, thay vào đó, nó gọi các phương thức của trẻ.
Output:
def multi_getattr[obj, attr, default = None]:
"""
Get a named attribute from an object; multi_getattr[x, 'a.b.c.d'] is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.
"""
attributes = attr.split["."]
for i in attributes:
try:
obj = getattr[obj, i]
except AttributeError:
if default:
return default
else:
raise
return obj
# Example usage
obj = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"
multi_getattr[obj, attr] #Will return the docstring for the
#capitalize method of the builtin string
#object
9& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số
Python3
Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python
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ố
Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python
Ví dụ 5: Chức năng Python getAttr []
Những gì tôi muốn có thể làm là khi một ví dụ của cha mẹ đề cập đến các thuộc tính của nó, thay vào đó, nó gọi các phương thức của trẻ.
Những gì tôi muốn có thể làm là khi một ví dụ của cha mẹ đề cập đến các thuộc tính của nó, thay vào đó, nó gọi các phương thức của trẻ.
Điều khác là tôi chỉ muốn hành vi này khi trường thuộc loại
getattr[myObj.contained, "c"]
9.Tôi đang gặp phải các vấn đề đệ quy cố gắng sử dụng
d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
0 và tương tự, dọc theo các dòng của:Việc đệ quy là khá rõ ràng tại sao nó xảy ra ... nhưng làm thế nào để tôi tránh nó?
Tôi đã thấy một vài ví dụ đã có trên StackOverflow, nhưng dường như tôi không thể tìm ra cách khắc phục nó.
Output:
my_attrs = [getattr[obj, attr] for attr in attr_list]
1Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python
Python3
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ố
Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python
Ví dụ 5: Chức năng Python getAttr []
Tôi đang gặp phải các vấn đề đệ quy cố gắng sử dụng
d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
0 và tương tự, dọc theo các dòng của:Output:
my_attrs = [getattr[obj, attr] for attr in attr_list]
3Ví dụ 5: Chức năng Python getAttr []
Python3
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ố
Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python
Ví dụ 5: Chức năng Python getAttr []
Những gì tôi muốn có thể làm là khi một ví dụ của cha mẹ đề cập đến các thuộc tính của nó, thay vào đó, nó gọi các phương thức của trẻ.
d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
51def __getattr__[self, key]:
if isinstance[getattr[self, key], Field]:
return getattr[self, key].get[]
return getattr[self, key]
4d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
68d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
69d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
70Điều khác là tôi chỉ muốn hành vi này khi trường thuộc loại
getattr[myObj.contained, "c"]
9.Tôi đang gặp phải các vấn đề đệ quy cố gắng sử dụng
d = Document[]
d.y = 'some value' # Calls the `set` function of the Field
d.y == 'some value' # Calls the `get` function of the Field
del d.y # Calls the `delete` function of the Field
0 và tương tự, dọc theo các dòng của:Output:
getattr[myObj.contained, "c"]
6Việc đệ quy là khá rõ ràng tại sao nó xảy ra ... nhưng làm thế nào để tôi tránh nó?Conventional method takes less time than getattr[], but when default values have to be used in case of missing attributes, getattr[] is a good choice.
Tôi đã thấy một vài ví dụ đã có trên StackOverflow, nhưng dường như tôi không thể tìm ra cách khắc phục nó.The 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.