Phương pháp kỳ diệu là gì?

Phương pháp ma thuật là gì? . Chúng luôn được bao quanh bởi hai dấu gạch dưới [e. g. __init__, __lt__]. Dấu gạch dưới kép chỉ ra rằng đây là các phương thức ma thuật và lập trình viên không nên gọi trực tiếp, chúng thường được gọi bởi chính trình thông dịch. Hãy xem xét một số ví dụ phổ biến về các phương pháp ma thuật này

ghi chú

Hướng dẫn này yêu cầu hiểu biết tốt về các lớp Python. Để đọc chúng, hãy kiểm tra tại đây. https. // tài liệu. con trăn. org/3/hướng dẫn/lớp học. html

Hướng dẫn không cung cấp một danh sách đầy đủ các phương pháp ma thuật. Nếu bạn muốn đọc thêm, hãy kiểm tra tài liệu.

Xây dựng và khởi tạo

Bạn có thể đã thấy phương pháp ma thuật cơ bản nhất, __init__. Đó là những gì chúng ta sử dụng để xác định việc khởi tạo một đối tượng. Tuy nhiên, nó không phải là phương thức đầu tiên được gọi khi một đối tượng mới được tạo. Phương thức đầu tiên được gọi là phương thức __new__ tạo một thể hiện mới và sau đó gọi phương thức __init__ khởi tạo đối tượng theo các tham số được truyền cho __new__ [mà nó truyền cho __init__]. 2 chức năng này cùng nhau tạo nên hàm tạo của một đối tượng

Phương thức thứ ba liên quan đến việc tạo và xóa đối tượng là phương thức

Testing ==
False
True
False
Testing !=
True
False
True
Testing

False
False
False
Testing =
False
True
False
1 được gọi khi vòng đời của đối tượng kết thúc. Nó thực hiện bất kỳ công việc dọn dẹp nào có thể cần thiết khi một đối tượng bị phá hủy. Nó là công cụ phá hủy đối tượng

Đại diện cho lớp bạn

Nó thường hữu ích khi có một chuỗi biểu diễn của một lớp. Python cung cấp một vài phương thức mà chúng ta có thể triển khai trong định nghĩa lớp của mình để tùy chỉnh cách các hàm tích hợp trả về các biểu diễn của hành vi lớp của chúng ta. Dưới đây là một vài trong số các chức năng này

  • Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    2 Xác định hành vi khi
    Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    3 được gọi trên một thể hiện của lớp học của bạn
  • Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    4 Xác định hành vi khi
    Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    5 được gọi trên một thể hiện của lớp học của bạn. Sự khác biệt chính giữa
    Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    3 và
    Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    5 là đối tượng mục tiêu. Đầu ra
    Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    5 phải có thể đọc được bằng máy [ví dụ JSON] trong khi đầu ra
    Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    3 phải có thể đọc được bằng con người

phương pháp so sánh

Python có khá nhiều phương thức kỳ diệu được thiết kế để thực hiện so sánh giữa các đối tượng bằng cách sử dụng toán tử thay vì gọi phương thức. Chúng cũng cung cấp một cách để ghi đè hành vi Python mặc định để so sánh các đối tượng [bằng cách tham khảo]. Ở đây chúng tôi liệt kê một số phương pháp kỳ diệu này và những gì chúng làm

  • class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    0 Đây là phương pháp ma thuật so sánh cơ bản nhất. Nó có thể thực hiện hành vi cho tất cả các toán tử so sánh [_______9_______1,
    class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    2,
    class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    3, v.v. ]. Nó sẽ trả về int âm nếu
    class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    4, 0 nếu
    class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    5 và int dương nếu
    class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    6. Tốt hơn hết là xác định từng phép so sánh mà chúng ta cần, từng cái một thay vì tất cả cùng một lúc nhưng
    class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    7 và phương pháp này thậm chí không còn được sử dụng nữa kể từ Python 3
  • class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    8 Xác định hành vi của toán tử đẳng thức
    class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    2
  • x: 7, y: 10
    x: 3, y: 4
    x: 3, y: 4
    x: 14, y: 21
    x: 10, y: 14
    0 Xác định hành vi của toán tử bất đẳng thức
    class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    3
  • x: 7, y: 10
    x: 3, y: 4
    x: 3, y: 4
    x: 14, y: 21
    x: 10, y: 14
    2 Xác định hành vi của toán tử nhỏ hơn
    class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    1
  • x: 7, y: 10
    x: 3, y: 4
    x: 3, y: 4
    x: 14, y: 21
    x: 10, y: 14
    4 Xác định hành vi của toán tử lớn hơn
    x: 7, y: 10
    x: 3, y: 4
    x: 3, y: 4
    x: 14, y: 21
    x: 10, y: 14
    5
  • x: 7, y: 10
    x: 3, y: 4
    x: 3, y: 4
    x: 14, y: 21
    x: 10, y: 14
    6 Xác định hành vi của toán tử nhỏ hơn hoặc bằng với
    x: 7, y: 10
    x: 3, y: 4
    x: 3, y: 4
    x: 14, y: 21
    x: 10, y: 14
    7
  • x: 7, y: 10
    x: 3, y: 4
    x: 3, y: 4
    x: 14, y: 21
    x: 10, y: 14
    8 Xác định hành vi của toán tử lớn hơn hoặc bằng
    x: 7, y: 10
    x: 3, y: 4
    x: 3, y: 4
    x: 14, y: 21
    x: 10, y: 14
    9
Thí dụ

Trong ví dụ sau, chúng tôi sẽ triển khai một lớp tùy chỉnh có tên là Khu vực sẽ lưu trữ chiều cao và chiều rộng của hình ảnh. Chúng tôi sẽ thực hiện nó với tất cả các phương pháp ma thuật so sánh để xem tất cả chúng hoạt động như thế nào

Đầu vào

class Area:

def __init__[self, height, width]:
self.height = height
self.width = width

def __eq__[self, other]:
if isinstance[other, Area]:
return self.height * self.width == other.height * other.width
else:
return False

def __ne__[self, other]:
return not self == other

def __lt__[self, other]:
if isinstance[other, Area]:
return self.height * self.width < other.height * other.width
else:
return False

def __gt__[self, other]:
if isinstance[other, Area]:
return self.height * self.width > other.height * other.width
else:
return False

def __le__[self, other]:
return self == other or self < other

def __ge__[self, other]:
return self == other or self > other

a1 = Area[7, 10]
a2 = Area[35, 2]
a3 = Area[8, 9]
print['Testing ==']
print[a1 == 'hello']
print[a1 == a2]
print[a1 == a3]
print['Testing !=']
print[a1 != 'hello']
print[a1 != a2]
print[a1 != a3]
print['Testing ']
print[a1 > 'hello']
print[a1 > a2]
print[a1 > a3]
print['Testing =']
print[a1 >= 'hello']
print[a1 >= a2]
print[a1 >= a3]

đầu ra

Testing ==
False
True
False
Testing !=
True
False
True
Testing

False
False
False
Testing =
False
True
False
Toán tử số học

Cũng giống như các phương thức so sánh, python cũng cho phép bạn quá tải các toán tử số học. Ở đây chúng ta sẽ xem xét một vài trong số chúng mà bạn có thể nạp chồng [ngoài ra, bạn có thể nạp chồng khá nhiều toán tử mà bạn muốn sử dụng trên một đối tượng]

toán tử đơn nguyên
  • __init__0 Thực hiện hành vi khi toán tử __init__1 unray được gọi trên đối tượng của chúng ta
  • __init__2 Thực hiện hành vi khi toán tử __init__3 unray được gọi trên đối tượng của chúng ta
Toán tử số học bình thường
  • __init__4 Thực hiện phép cộng với toán tử __init__1
  • __init__6 Thực hiện phép trừ với toán tử __init__3
  • __init__8 Thực hiện phép nhân với toán tử __init__9
  • __lt__0 Triển khai bitwise 'and' với toán tử __lt__1
  • __lt__2 Triển khai bitwise 'or' với toán tử __lt__3
  • __lt__4 Triển khai bitwise 'xor' với toán tử __lt__5
Toán tử số học được phản ánh
  • __lt__6 Thực hiện toán tử cộng __init__1 với các toán hạng được hoán đổi
  • __lt__8 Triển khai toán tử trừ __init__3 với các toán hạng được hoán đổi
Thí dụ

Trong ví dụ này, chúng ta sẽ tạo một lớp đại diện cho một Điểm với 2 tọa độ của nó. Chúng ta sẽ triển khai các toán tử cộng và trừ với Điểm và chúng ta cũng sẽ triển khai toán tử nhân với các số float sẽ có tính chất giao hoán. Cuối cùng, chúng ta cũng sẽ triển khai toán tử đơn nguyên __init__3, điều này sẽ phủ định cả hai tọa độ của một Điểm. Chúng tôi cũng sẽ triển khai __init__1 để có thể dễ dàng in ra

Đầu vào

class Point:

def __init__[self, x, y]:
self.x = x
self.y = y

def __add__[self, other]:
if isinstance[other, Point]:
return Point[self.x + other.x, self.y + other.y]
else:
raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

def __sub__[self, other]:
if isinstance[other, Point]:
return Point[self.x - other.x, self.y - other.y]
else:
raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

def __mul__[self, other]:
if isinstance[other, float] or isinstance[other, int]:
return Point[self.x * other, self.y * other]
else:
raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

def __rmul__[self, other]:
return self * other

def __neg__[self]:
return self * -1

def __str__[self]:
return 'x: {}, y: {}'.format[self.x, self.y]

a = Point[2, 3]
b = Point[5, 7]
print[a + b]
print[b - a]
print[-[a - b]]
print[a * 7]
print[2 * b]

đầu ra

x: 7, y: 10
x: 3, y: 4
x: 3, y: 4
x: 14, y: 21
x: 10, y: 14
Tóm tắt

Chúng ta đã thấy khá nhiều phương thức ma thuật khác nhau mà chúng ta có thể quá tải và cách chúng thực sự thay đổi hành vi của lớp chúng ta. Đây chỉ là một tập hợp con nhỏ của các loại hành vi khác nhau mà chúng ta có thể thay đổi trong đối tượng của mình. Liệt kê bên dưới, chúng ta có thể thấy tất cả các loại phương thức ma thuật mà chúng ta có thể quá tải và một số ví dụ, danh sách này vẫn chưa đầy đủ, vì vậy hãy kiểm tra tài liệu Python để xem tất cả chúng

Xây dựng và khởi tạo

  • __init__2
  • __init__3

so sánh

  • class Point:

    def __init__[self, x, y]:
    self.x = x
    self.y = y

    def __add__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x + other.x, self.y + other.y]
    else:
    raise ValueError['Addition with type {} is undefined.'.format[type[other]]]

    def __sub__[self, other]:
    if isinstance[other, Point]:
    return Point[self.x - other.x, self.y - other.y]
    else:
    raise ValueError['Subtraction with type {} is undefined.'.format[type[other]]]

    def __mul__[self, other]:
    if isinstance[other, float] or isinstance[other, int]:
    return Point[self.x * other, self.y * other]
    else:
    raise ValueError['Multiplication with type {} is undefined.'.format[type[other]]]

    def __rmul__[self, other]:
    return self * other

    def __neg__[self]:
    return self * -1

    def __str__[self]:
    return 'x: {}, y: {}'.format[self.x, self.y]

    a = Point[2, 3]
    b = Point[5, 7]
    print[a + b]
    print[b - a]
    print[-[a - b]]
    print[a * 7]
    print[2 * b]
    8
  • x: 7, y: 10
    x: 3, y: 4
    x: 3, y: 4
    x: 14, y: 21
    x: 10, y: 14
    0

Các toán tử và hàm một ngôi

  • Toán tử __init__0 đơn nguyên __init__1
  • Toán tử __init__2 một ngôi __init__3

Toán tử số học bình thường

  • __init__4
  • __init__6

Toán tử số học được phản ánh

  • Toán tử bổ sung __lt__6 __init__1 với các toán hạng được hoán đổi
  • __lt__8 toán tử trừ __init__3 với các toán hạng được đổi chỗ

bài tập tăng cường

  • Phép cộng __new__6 với phép gán sử dụng toán tử __new__7
  • Phép trừ __new__8 với phép gán sử dụng toán tử __new__9

loại chuyển đổi

  • Chuyển đổi loại __init__0 thành __init__1 với phương thức __init__2
  • Chuyển đổi loại __init__3 thành __init__4 với phương pháp __init__5

Đại diện cho lớp bạn

  • Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    2 biểu diễn chuỗi của lớp với phương thức
    Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    3
  • Testing ==
    False
    True
    False
    Testing !=
    True
    False
    True
    Testing

    False
    False
    False
    Testing =
    False
    True
    False
    4 biểu diễn lớp có thể đọc được bằng máy với phương thức __init__9

Thông tin về các Tác giả

Tamas làm việc với tư cách là Kỹ sư dữ liệu tại Cambridge Spark, phát triển đường dẫn dữ liệu của công ty để xử lý dữ liệu chảy qua hệ thống của chúng tôi. Trước đó, anh ấy đã làm việc tại Morgan Stanley trong 2 năm để phát triển các dịch vụ ngăn chặn gian lận theo thời gian thực, thông lượng cao với sự hợp tác chặt chẽ với các Nhà khoa học dữ liệu. Anh ấy có bằng Cử nhân Phát triển Phần mềm và đang theo học Thạc sĩ tại Đại học Birkbeck ở London về Công nghệ Điện toán Tiên tiến

Kết nối với Tamas

Cảm ơn vì đã đọc. Nếu bạn thích bài đăng này, chúng tôi đánh giá cao sự hỗ trợ của bạn bằng cách vỗ tay thông qua nút vỗ tay [👏🏼] bên dưới hoặc bằng cách chia sẻ bài viết này để những người khác có thể tìm thấy nó

Nếu bạn muốn là người đầu tiên biết về nội dung mới của chúng tôi, bao gồm các hướng dẫn mới, nghiên cứu điển hình và các tài nguyên hữu ích khác, hãy nhấp vào đây

Bạn có thể cho một ví dụ về một phương pháp kỳ diệu?

Một số ví dụ về phương pháp ma thuật là __init__, __len__, __repr__, __add__ , v.v.

__ Le __ một phương pháp ma thuật để làm gì?

Ví dụ: khi bạn cộng hai số bằng toán tử +, bên trong, phương thức __add__[] sẽ được gọi. Các lớp tích hợp trong Python định nghĩa nhiều phương thức ma thuật. . Phương pháp ma thuật quan trọng

Phương pháp kỳ diệu trong PHP là gì?

Các phương thức ma thuật trong PHP là các phương thức đặc biệt nhằm thực hiện một số tác vụ nhất định . Các phương thức này được đặt tên với dấu gạch dưới kép [__] làm tiền tố. Tất cả các tên chức năng này được bảo lưu và không thể được sử dụng cho bất kỳ mục đích nào khác ngoài chức năng ma thuật liên quan. Phương thức ma thuật trong một lớp phải được khai báo công khai.

Tên thứ hai của phương pháp ma thuật là gì?

Đây là lý do tại sao các phương thức ma thuật còn được gọi là “ các phương thức dunder ”, viết tắt của “Các phương thức gạch dưới đôi. ” Trong đoạn mã trên, bạn có thể thấy những gì tôi đang nói về. Python thay mặt bạn gọi phương thức __init__ dunder khi một thể hiện lớp mới được tạo.

Chủ Đề