Thừa kế phân cấp trong python là gì?

Trong bài học trước, bạn đã đề cập đến các đối tượng và kế thừa. Trong bài học này, bạn sẽ tìm hiểu sâu hơn về ghi đè phương thức và xem cách sử dụng

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 để truy cập các phương thức cha được ghi đè

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return 2 * self.length + 2 * self.width

    def what_am_i(self):
        return 'Rectangle'

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def what_am_i(self):
        return 'Square'

class Cube(Square):
    def surface_area(self):
        face_area = self.area()
        return face_area * 6

    def volume(self):
        face_area = super().area()
        return face_area * self.length

    def what_am_i(self):
        return 'Cube'

    def family_tree(self):
        return self.what_am_i() + ' child of ' + super(Cube, self).what_am_i() 

00. 00 Ở bài trước mình đã hướng dẫn các bạn sử dụng tính kế thừa đối tượng trong Python. Trong bài học này, tôi sẽ chỉ cho bạn cách sử dụng

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 để truy cập các phương thức trong đối tượng cha theo hệ thống phân cấp thừa kế. Nếu bạn đang viết mã cùng với tôi, tôi vẫn đang sử dụng
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
3 cho mã của mình. Trước hết, tôi sẽ thêm
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4 vào
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
3

00. 21 Cũng giống như

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1,
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4 chỉ yêu cầu một
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
3 để khởi tạo và vì tôi đang thừa kế từ
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1 nên tôi không phải xác định lại
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
5. Tôi chỉ có thể sử dụng của
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1

00. 32

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4 có hai phương thức không có trong cha mẹ của nó
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1. Đầu tiên là
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
9, và thứ hai là
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
0

00. 40 Cả hai đều sử dụng hàm

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 từ cha mẹ, nhưng tôi đang chỉ ra hai cách khác nhau để hoàn thành cùng một việc. Trong
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
9,
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
3 được gọi là. Python tìm kiếm một phương thức có tên là
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 bên trong
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4

00. 55 Khi không tìm thấy, nó sẽ vào

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1 để tìm kiếm điều tương tự. Nó cũng sẽ không tìm thấy nó trong
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1. Nếu bạn còn nhớ,
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 được định nghĩa trong
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
9. Khi tìm thấy nó trong
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
9, nó sẽ trả về kết quả và sau đó chúng tôi nhân kết quả đó với
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
81 để có được diện tích bề mặt của chúng tôi.
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
0 đang làm một điều rất giống nhau. Tuy nhiên, trong trường hợp này, thay vì nhìn vào đối tượng hiện tại, nó nhìn vào siêu đối tượng, có nghĩa là nó nhìn vào
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1 cho
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1

01. 23 Nó vẫn không tìm thấy nó. Nó đi lên từ đó đến

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
9 và sau đó tìm thấy nó ở đó và cung cấp cho chúng tôi
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
86 cho tập. Và bây giờ nếu chúng tôi mở REPL, chúng tôi có thể nhập
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4 như trước đây, tạo
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4 với
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
3 của
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
50,

01. 39 gọi cho

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
9,

01. 41 gọi cho

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
0, và chúng tôi nhận được kết quả như mong đợi

01. 46 Vì vậy, hãy xem lại cách các phương thức được gọi trên các đối tượng trong Python. Trước hết, Python nhìn vào đối tượng hiện tại để xem liệu một phương thức có tên được gọi có tồn tại hay không. Nếu vậy, tuyệt vời, nó gọi nó

01. 58 Nếu không, nó sẽ chuyển đến cha của đối tượng đó để xem có phương thức nào có cùng tên đó không. Nó tiếp tục làm điều này cho đến khi nó tìm thấy nó và gọi nó, hoặc nó hết chuỗi thừa kế. Nếu nó hết chuỗi thừa kế, nó sẽ ném một

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
53. Để cho bạn thấy điều này trong thực tế, tôi sẽ mở
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
3 và sửa đổi hình dạng của
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
9

02. 20 Tôi đang thêm một phương thức có tên là

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
56 không làm gì khác ngoài việc trả về một chuỗi đơn giản có từ
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
57 trong đó. Bên trong REPL, tôi sẽ nhập một
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
9 và tạo nó, rồi gọi
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
56

02. 33 Nó trả về chuỗi

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
57. Và điều gì sẽ xảy ra nếu bạn làm điều tương tự với
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1?

02. 44 Để thay đổi điều này, mã bên trong của

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1 cũng sẽ phải thay đổi. Để
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1 trả về một kết quả nhạy cảm hơn, tôi đã ghi đè phương thức
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
56 của nó. Sau đó tôi đã làm điều tương tự cho
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4

02. 56 Bây giờ từ REPL, nếu tôi tạo một

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
9, không có gì thay đổi đối với nó—nó vẫn báo cáo
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
57. Nhưng khi
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
11 được gọi, bởi vì có một phương thức
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
56 trên chính đối tượng
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1, nên phương thức bị ghi đè sẽ được gọi. Tương tự như vậy đối với
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4, phương thức
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4 bị ghi đè được gọi và bây giờ kết quả trông giống như những gì bạn có thể mong đợi. Giả sử từ bên trong đối tượng
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4, bạn muốn truy cập phương thức
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
56 của đối tượng
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1.
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 là thứ sẽ cho phép bạn làm điều đó

03. 27

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 cung cấp cho bạn quyền truy cập vào đối tượng gốc và các phương thức bên trong. Nó có thể được gọi theo một số cách khác nhau—ngay lập tức mà không có bất kỳ tham số nào, nhưng cũng có hai tham số. Với hai tham số, cần có một
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
11 và một
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
12. Trong trường hợp này, bạn thậm chí không cần phải ở bên trong đối tượng để nó hoạt động

03. 46 Nếu bạn đang ở trong một lớp, thì

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 không có bất kỳ tham số nào thực sự chỉ là lối tắt cho đối tượng
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
14 và đối tượng
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
15. Hãy xem nó trong thực tế bằng cách nhìn vào đối tượng
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4

04. 00 Nhập

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4, tạo nó, đặt tên cho nó là
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
56, bạn sẽ nhận thấy
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
19. Chúng tôi đã thấy điều này trước đây. Bây giờ, nếu tôi gọi
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 đi qua lớp
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4 và đối tượng
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
32 cho
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
56,

04. 14 Python đi lên cha của

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4, là
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1, và gọi phương thức của nó. Tương tự như vậy, nếu chúng ta truyền nó vào với lớp
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1 và đối tượng
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
32, nó sẽ chuyển đến cha của
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1 và trả về kết quả từ đó

04. 33 Đây là cách

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 cho phép bạn lấy bất kỳ phương thức nào bị ghi đè trong hệ thống phân cấp thừa kế. Hãy xem đây là một ví dụ bên trong một phương thức trong lớp
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4. Phương thức mới này
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
41 trả về một chuỗi báo cáo “Tôi là ai, và tôi là con của. ”
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
15 trả về
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
56 cho
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4, và
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 trong trường hợp này sẽ trả về
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
56 của lớp
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1. Bạn sẽ nhận thấy rằng tôi đang sử dụng phiên bản dạng ngắn của
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
1 ở đây, không cần phải chỉ định lớp
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4 và
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
15, vì tôi đang ở trong một phương thức của đối tượng

05. 12 Hãy tạo

# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
32. Khi
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
41 được gọi,
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
03 được báo cáo, gọi
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
56 của
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
4 và
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
06 của
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
1. Cho đến nay, tất cả các ví dụ của chúng tôi đều là kế thừa đơn lẻ. Trong bài tiếp theo, tôi sẽ chỉ cho bạn cách sử dụng đa thừa kế và những phức tạp phát sinh từ đó

Thừa kế phân cấp trong python là gì?

Brandon vào tháng 1. 29, 2020

cảm ơn vì video. Một vấn đề, mặc dù. Tôi liên tục nhận được AttributeError khi gọi khối. âm lượng(). loại đối tượng 'siêu' không có thuộc tính 'khu vực' Thể hiện khối lập phương của Cube có thuộc tính "khu vực", nhưng nó gây ra lỗi khi tìm kiếm nó trong (các) phần tử cha. Khi tôi thay thế

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
08 bằng
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
09, khối lập phương. âm lượng () hoạt động tốt. Bất kỳ suy nghĩ?

Brandon vào tháng 1. 29, 2020

Có vẻ như sự cố có thể xảy ra với việc nhập vào thiết bị đầu cuối iPython. Khi tôi chạy mã trong Cửa sổ tương tác Python trong Mã VS thì không sao, nhưng gọi khối lập phương. volume() sau khi nhập qua thiết bị đầu cuối (vào iPython) sẽ báo lỗi. Ồ tốt, không phải lo lắng. Cảm ơn

Thừa kế phân cấp trong python là gì?

Đánh dấu vào ngày 15 tháng 6 năm 2020

Xin chào mọi người, xin lỗi nếu câu hỏi này là ngu ngốc. Tôi hơi khó khăn, khi tôi nhập lớp Cha mẹ thì tất cả đều ổn nhưng nếu tôi nhập lớp con thì tôi nhận được khu vực này

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given

Thừa kế phân cấp trong python là gì?

Christopher Trudeau Nhóm RP vào ngày 16 tháng 6 năm 2020

Xin chào UgandanGuy,

Tôi chỉ đoán thôi vì tôi không thể thấy mã của bạn, nhưng nếu bạn kế thừa từ Hình chữ nhật thay vì Hình vuông, bạn có thể thấy thông báo này. Hình chữ nhật nhận hai đối số (có nghĩa là 3 vì “self”), trong khi Square chỉ nhận một (có nghĩa là 2 vì “self”)

Kiểm tra kỹ bạn có

lớp Khối lập phương (Hình vuông). . .

…ct

Thừa kế phân cấp trong python là gì?

Thomas J Foolery vào ngày 23 tháng 6 năm 2020

Cảm ơn Christopher. tôi thích video. Mình làm bên bạn nhưng ko copy nguyên văn. Đồ chơi nhỏ của tôi trông như thế này

________số 8

Đầu ra trông như thế này

Rectangle of length 2 and width 4 has area 8 and perimiter 12 Square with sides = 6 has area 36 and perimeter > rectangle with length = 2 and width = 4 square with side-length = 6

Quá trình kết thúc với mã thoát 0

Có vẻ như phương pháp chu vi không được kế thừa như mong đợi. Tôi đang sử dụng PyCharm với Python 3. 6. Bất kỳ suy nghĩ?

Christopher Trudeau Nhóm RP vào ngày 23 tháng 6 năm 2020

Chào Thomas,

Bạn đã bỏ qua dấu ngoặc gọi hàm đến chu vi. Đầu ra mà bạn đang thấy là Python cho bạn biết về hàm chứ không phải gọi hàm

Thay đổi dòng của bạn thành này

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
8

Và bạn nên tốt để đi

Thừa kế phân cấp trong python là gì?

davevikram vào ngày 23 tháng 6 năm 2020

Xin chào Chris,

Tôi đã làm theo hướng dẫn và mọi thứ đều ổn cho đến khi tôi tạo lớp Cube và tôi không thể vượt qua lỗi này. Tôi thậm chí đã sao chép mã từ trên và vẫn tiếp tục gặp lỗi này

Nhập Lỗi. không thể nhập tên 'Cube'

from shapes import Cube Traceback (most recent call last): File “”, line 1, in ImportError: cannot import name ‘Cube’

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
5

Thừa kế phân cấp trong python là gì?

Dan B vào tháng 11. 13, 2020

davevikram là bạn trong REPL?

Dan B vào tháng 11. 13, 2020

lúc 4. 44 bạn nói,

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
10, nhưng tôi nghĩ trong bối cảnh này, ý bạn là
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
11?

Christopher Trudeau Nhóm RP vào tháng 11. 13, 2020

Chào Đan,

Trước hết, không, tôi không sử dụng REPL. Tôi đã cắt và dán mã từ REPL vào một slide. Bạn đúng là REPL cơ sở không cho phép nhập lại, vì vậy nếu bạn cần nhập lại, bạn sẽ phải khởi động lại

Nhận xét trong mã mẫu

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
10

thực sự đúng. Lệnh gọi super() là lối tắt của lệnh gọi super với lớp hiện tại. Trong ví dụ Cube là lớp hiện tại. Kết quả của cuộc gọi này là phương thức của Square. Hãy coi super() là viết tắt của “parent of”. Trong trường hợp này, chúng tôi đang tìm kiếm "cha mẹ của Cube", đó là Square. Gọi đến phím tắt “super()” là viết tắt của “parent of self’s class”

Thừa kế phân cấp trong python là gì?

Konstantinos vào tháng 10. 5, 2022

Xin chào, tôi đang cố gắng hiểu cách thích hợp để gọi các thuộc tính cấp lớp của lớp cha từ một phương thức trong lớp con

Trong ví dụ sau tôi thấy có thể sử dụng

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
13,
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
14,
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
15 để lấy thuộc tính color từ lớp cha trong phương thức
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
16 của lớp con. Tôi đã tự hỏi cuộc gọi nào là hợp lệ nhất hoặc liệu chúng có tương đương nhau không?

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
3

Cảm ơn

Christopher Trudeau Nhóm RP vào tháng 10. 5, 2022

Xin chào Konstantinos,

Ý tưởng đằng sau kế thừa đối tượng là đứa trẻ lấy mọi thứ từ cha mẹ. Lý do duy nhất để sử dụng

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
17 là nếu bạn đang cố gắng đạt được điều gì đó ở cha mẹ chứ không phải ở con cái. Điều này chỉ xảy ra nếu bạn đang ghi đè

Bạn hiếm khi thấy

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
17 được sử dụng với các thuộc tính, nó hầu như luôn được sử dụng với các phương thức mà cha mẹ có cách triển khai khác với con. Cách sử dụng phổ biến nhất khi ghi đè
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
19 trong đối tượng con và muốn gọi phương thức cùng tên của cha mẹ

Bạn có thể sử dụng

from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
17 với các thuộc tính, ví dụ nếu cả
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
21 và
# in shapes.py
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return (self.length + self.width) * 2

    def __str__(self):
        return "rectangle with length = {} and width = {}".format(self.length, self.width)

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def __str__(self):
        return "square with side-length = {}".format(self.length)


rect = Rectangle(2,4)
print("Rectangle of length {} and width {} has area {} and perimiter {}".format(rect.length, rect.width, rect.area(), rect.perimeter()))

sq = Square(6)
print("Square with sides = {} has area {} and perimeter {}".format(sq.length, sq.area(), sq.perimeter))

print(rect)
print(sq)
9 đều khai báo
from shapes import Rectangle, Square, Cube
>>> cu = Cube(3)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/real_python_project/int/shapes.py", line 15, in __init__
    super().__init__(length, length)  # accessing the parent method intialisation
TypeError: __init__() takes 2 positional arguments but 3 were given
23. Nhưng trong thực tế, nếu bạn đang thay đổi các loại hằng số đó, có lẽ bạn muốn có một biến thể hiện hơn là một cấp độ lớp. Tôi chắc rằng có những trường hợp ngoại lệ ngoài kia, tôi chỉ không thấy nó được sử dụng thường xuyên trong mã

Thừa kế phân cấp là gì?

Kế thừa phân cấp mô tả tình huống trong đó một lớp cha được kế thừa bởi nhiều lớp con . Một kiểu kế thừa trong đó nhiều hơn một lớp được kế thừa từ một lớp cha hoặc lớp cơ sở được gọi là kế thừa phân cấp.

Kế thừa phân cấp giải thích với ví dụ là gì?

Trong kế thừa phân cấp, tất cả các tính năng phổ biến trong các lớp con đều được bao gồm trong lớp cơ sở . Ví dụ: Vật lý, Hóa học, Sinh học được lấy từ lớp Khoa học. Tương tự, Dog, Cat, Horse có nguồn gốc từ lớp Animal.

Làm thế nào để python thực hiện kế thừa phân cấp?

Kế thừa phân cấp trong Python .
Chương trình #Python thể hiện tính kế thừa phân cấp
#Lớp cơ bản
lớp thú cưng
def __init__(self, pet_type, name, bdate)
bản thân. pet_type = pet_type
bản thân. tên = tên
bản thân. ngày = ngày
def chi tiết (bản thân)

Kế thừa lai trong python là gì?

Thừa kế kết hợp là sự pha trộn của nhiều loại thừa kế . Lớp được dẫn xuất từ ​​hai lớp như trong đa kế thừa. Tuy nhiên, một trong các lớp cha không phải là lớp cơ sở. Nó là một lớp dẫn xuất. Tính năng này cho phép người dùng sử dụng tốt nhất tính năng kế thừa.