Hướng dẫn python getattr class - lớp getattr python

Tôi có một lớp học là một trường hợp của một lớp khác.

Nội phân chính

  • Cách thức hoạt động của GetAttr ()
  • Ví dụ 1: Thể hiện hoạt động của getAttr () & nbsp;
  • 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 Field.

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:

def __getattr__(self, key):
    if isinstance(getattr(self, key), Field):
        return getattr(self, key).get()
    return getattr(self, key)

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

  • Lỗi độ sâu đệ quy tối đa với GetAttr
  • Hiểu được sự khác biệt giữa __getAttr__ và __getAttribution__

Hàm python getAttr () được sử dụng để truy cập giá trị thuộc tính của một đối tượng và cũng cung cấp tùy chọn thực hiện giá trị mặc định trong trường hợp không có sẵn khóa. 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.

Cú pháp: GetAttr (obj, key, def) getattr(obj, key, def)

Tham số: & nbsp;

  • OBJ: Đối tượng có thuộc tính cần được xử lý.The object whose attributes need to be processed.
  • Khóa: Thuộc tính của đối tượngThe attribute of object
  • DEF: Giá trị mặc định cần được in trong thuộc tính trường hợp không được tìm thấy.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. 

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

The name is GeeksforGeeks
Description is CS Portal
8
The name is GeeksforGeeks
Description is CS Portal
9
AttributeError: GfG instance has no attribute 'motto'
0

The name is GeeksforGeeks
Description is CS Portal
8
AttributeError: GfG instance has no attribute 'motto'
2
The name is GeeksforGeeks
Description is CS Portal
1

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:  

The name is GeeksforGeeks
Description is CS Portal

Exception: 

AttributeError: GfG instance has no attribute 'motto'

Ví 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:

AttributeError: 'GfG' object has no attribute 'gender'

& 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 Field.

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:  

The name is GeeksforGeeks
Time to execute getattr 5.0067901611328125e-06
The name is GeeksforGeeks
Time to execute conventional method 1.1920928955078125e-06

Ví 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:

AttributeError: 'GfG' object has no attribute 'motto'

Ví 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
51
def __getattr__(self, key):
    if isinstance(getattr(self, key), Field):
        return getattr(self, key).get()
    return getattr(self, key)
4
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
68
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
69
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
70

Điều khác là tôi chỉ muốn hành vi này khi trường thuộc loại Field.

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:

def __getattr__(self, key):
    if isinstance(getattr(self, key), Field):
        return getattr(self, key).get()
    return getattr(self, key)
4
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
81

def __getattr__(self, key):
    if isinstance(getattr(self, key), Field):
        return getattr(self, key).get()
    return getattr(self, key)
4
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
83

def __getattr__(self, key):
    if isinstance(getattr(self, key), Field):
        return getattr(self, key).get()
    return getattr(self, key)
4
def __getattr__(self, key):
    if isinstance(getattr(self, key), Field):
        return getattr(self, key).get()
    return getattr(self, key)
5
def __getattr__(self, key):
    if isinstance(getattr(self, key), Field):
        return getattr(self, key).get()
    return getattr(self, key)
8
The name is GeeksforGeeks
Description is CS Portal
7
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
88
The name is GeeksforGeeks
Description is CS Portal
1

def __getattr__(self, key):
    if isinstance(getattr(self, key), Field):
        return getattr(self, key).get()
    return getattr(self, key)
8
The name is GeeksforGeeks
Description is CS Portal
7
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
88
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
93
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
94
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
70

Output:

<__main__.GfG object at 0x0000023C1ED92748>

>
Vivek called with parameters 'arg'

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.

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.