Hướng dẫn does order of inheritance matter in python - thứ tự thừa kế trong python

Tôi có một lớp ExampleSim kế thừa từ lớp cơ sở Physics:

class Physics(object):
    arg1 = 'arg1'
    def physics_method:
        print 'physics_method'

class ExampleSim(Physics):
    print 'example physics sim'

Hãy tưởng tượng các lớp này chứa rất nhiều mã. Bây giờ tôi đã thực hiện một số sửa đổi thành Physics bằng cách xác định một lớp mới

class PhysicsMod(Physics):
    arg1 = 'modified arg1'
0 và kế thừa từ Physics:

class PhysicsMod(Physics):
    arg1 = 'modified arg1'

mà còn cho ExampleSim mà tôi đã tạo ra một lớp mới

class PhysicsMod(Physics):
    arg1 = 'modified arg1'
3 và được kế thừa từ ExampleSim:

class ExampleSimMod(ExampleSim):
    print 'modified example sim'

Vấn đề của tôi là

class PhysicsMod(Physics):
    arg1 = 'modified arg1'
3 kế thừa từ ExampleSim kế thừa từ Physics nơi tôi muốn có nó được thừa kế từ
class PhysicsMod(Physics):
    arg1 = 'modified arg1'
0 thay thế. Có cách nào để làm việc này không? Có lẽ thông qua
class PhysicsMod(Physics):
    arg1 = 'modified arg1'
9 hoặc bởi nhiều kế thừa?

class ExampleSimMod(ExampleSim, PhysicsMod):
    print 'modified example sim'

Thứ tự phân giải phương thức: & NBSP; Thứ tự phân giải phương thức (MRO) Nó biểu thị cách ngôn ngữ lập trình giải quyết một phương thức hoặc thuộc tính. Python hỗ trợ các lớp kế thừa từ các lớp khác. Lớp được kế thừa được gọi là cha mẹ hoặc siêu lớp, trong khi lớp kế thừa được gọi là con hoặc lớp con. Trong Python, thứ tự phân giải phương thức xác định thứ tự trong đó các lớp cơ sở được tìm kiếm khi thực hiện một phương thức. Đầu tiên, phương thức hoặc thuộc tính được tìm kiếm trong một lớp và sau đó nó tuân theo thứ tự chúng tôi đã chỉ định trong khi kế thừa. Thứ tự này còn được gọi là tuyến tính hóa của một lớp và tập hợp các quy tắc được gọi là MRO (thứ tự phân giải phương thức). Trong khi kế thừa từ một lớp khác, trình thông dịch cần một cách để giải quyết các phương thức được gọi thông qua một thể hiện. Do đó, chúng ta cần thứ tự phân giải phương pháp. Ví dụ: & nbsp; & nbsp; 
Method Resolution Order(MRO) it denotes the way a programming language resolves a method or attribute. Python supports classes inheriting from other classes. The class being inherited is called the Parent or Superclass, while the class that inherits is called the Child or Subclass. In python, method resolution order defines the order in which the base classes are searched when executing a method. First, the method or attribute is searched within a class and then it follows the order we specified while inheriting. This order is also called Linearization of a class and set of rules are called MRO(Method Resolution Order). While inheriting from another class, the interpreter needs a way to resolve the methods that are being called via an instance. Thus we need the method resolution order. For Example 
 

Python3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
1

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
3
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
4
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
5
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
6

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
7
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
9__
class ExampleSimMod(ExampleSim, PhysicsMod):
    print 'modified example sim'
1

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class PhysicsMod(Physics):
    arg1 = 'modified arg1'
07

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
3
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
4
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
5
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
6

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
7
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
9__
 In class B
3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class PhysicsMod(Physics):
    arg1 = 'modified arg1'
11

 In class B
7

Output:   
 

 In class B

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
3
class PhysicsMod(Physics):
    arg1 = 'modified arg1'
50
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
5
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
6
The order that follows in the above code is- class B – > class A 
In multiple inheritances, the methods are executed based on the order specified while inheriting the classes. For the languages that support single inheritance, method resolution order is not interesting, but the languages that support multiple inheritance method resolution order plays a very crucial role. Let’s look over another example to deeply understand the method resolution order: 
 

Python3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
1

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
3
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
4
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
5
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
6

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
7
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
9__
class ExampleSimMod(ExampleSim, PhysicsMod):
    print 'modified example sim'
1

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class ExampleSimMod(ExampleSim, PhysicsMod):
    print 'modified example sim'
3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
3
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
4
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
5
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
6

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
7
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
9__
class ExampleSimMod(ExampleSim, PhysicsMod):
    print 'modified example sim'
1

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class ExampleSimMod(ExampleSim, PhysicsMod):
    print 'modified example sim'
3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
3
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
4
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
5
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
6

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
7
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
9__
class ExampleSimMod(ExampleSim, PhysicsMod):
    print 'modified example sim'
1

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class ExampleSimMod(ExampleSim, PhysicsMod):
    print 'modified example sim'
3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2Physics7

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
7
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
9__
 In class B
3

 In class B
7

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0 ExampleSim3 
 

 In class B

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
7
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8Physics1
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0 Physics3Diamond inheritance and it looks as follows: 
 

Hướng dẫn does order of inheritance matter in python - thứ tự thừa kế trong python

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0 Physics5
Class D -> Class B -> Class C -> Class A 
Python follows depth-first order to resolve the methods and attributes. So in the above example, it executes the method in class B. 
  
Old and New Style Order : 
In the older version of Python(2.1) we are bound to use old-style classes but in Python(3.x & 2.2) we are bound to use only new classes. New style classes are the ones whose first parent inherits from Python root ‘object’ class. 
 

Python3

 In class B
4
 In class B
5 Physics0

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2Physics7

Đầu ra: & nbsp; & nbsp;

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2Physics7

Trong ví dụ trên, chúng tôi sử dụng nhiều kế thừa và nó còn được gọi là kế thừa kim cương và nó trông như sau: & nbsp; & nbsp;DLR or depth-first left to right algorithm whereas new style classes use C3 Linearization algorithm for method resolution while doing multiple inheritances. 
  
DLR Algorithm 
During implementing multiple inheritances, Python builds a list of classes to search as it needs to resolve which method has to be called when one is invoked by an instance. As the name suggests, the method resolution order will search the depth-first, then go left to right. For Example 
 

Python3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
1

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2Physics7

Python tuân theo thứ tự tra cứu đầu tiên và do đó cuối cùng gọi phương thức từ lớp A. Bằng cách tuân theo thứ tự độ phân giải phương thức, thứ tự tra cứu như sau. tuân theo thứ tự đầu tiên để giải quyết các phương thức và thuộc tính. Vì vậy, trong ví dụ trên, nó thực hiện phương thức trong lớp B. & nbsp; & nbsp; & nbsp; thứ tự kiểu cũ và mới: & nbsp; trong phiên bản cũ của Python (2.1), chúng tôi buộc phải sử dụng các lớp kiểu cũ nhưng trong Python ( 3.x & 2.2) Chúng tôi bị ràng buộc chỉ sử dụng các lớp mới. Các lớp phong cách mới là những lớp mà cha mẹ đầu tiên thừa hưởng từ python root ’lớp đối tượng. & Nbsp; & nbsp;

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2Physics7

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0 Physics3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2Physics7

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0 Physics7Physics8
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
6

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2Physics7

Thứ tự phân giải phương pháp (MRO) trong cả hai kiểu khai báo là khác nhau. Các lớp kiểu cũ sử dụng DLR hoặc độ sâu đầu tiên từ trái sang phải thuật toán trong khi các lớp kiểu mới sử dụng thuật toán tuyến tính hóa C3 để phân giải phương thức trong khi thực hiện nhiều kế thừa. Tìm kiếm vì nó cần giải quyết phương thức nào phải được gọi khi một người được gọi bởi một thể hiện. Như tên cho thấy, thứ tự độ phân giải phương thức sẽ tìm kiếm độ sâu đầu tiên, sau đó đi sang trái sang phải. Ví dụ: & nbsp; & nbsp;

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2Physics7

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class PhysicsMod(Physics):
    arg1 = 'modified arg1'
07
  
C3 Linearization Algorithm : 
C3 Linearization algorithm is an algorithm that uses new-style classes. It is used to remove an inconsistency created by DLR Algorithm. It has certain limitation they are: 
 

  • class ExampleSimMod(ExampleSim):
        print 'modified example sim'
    
    0
    class PhysicsMod(Physics):
        arg1 = 'modified arg1'
    
    11
  • class ExampleSimMod(ExampleSim):
        print 'modified example sim'
    
    0
    class PhysicsMod(Physics):
        arg1 = 'modified arg1'
    
    15

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class PhysicsMod(Physics):
    arg1 = 'modified arg1'
19
 

  • Trong thuật toán ví dụ trên trước tiên xem xét lớp thể hiện cho phương thức được gọi. Nếu không có mặt, thì nó nhìn vào cha mẹ đầu tiên, nếu điều đó cũng không có mặt sau đó là cha mẹ của cha mẹ được nhìn vào. Điều này tiếp tục cho đến khi kết thúc độ sâu của lớp và cuối cùng, cho đến khi kết thúc các lớp kế thừa. Vì vậy, thứ tự độ phân giải trong ví dụ cuối cùng của chúng tôi sẽ là D, B, A, C, A. Nhưng, A không thể có mặt hai lần như vậy, thứ tự sẽ là D, B, A, C. Nhưng thuật toán này khác nhau theo những cách khác nhau và Hiển thị các hành vi khác nhau tại các thời điểm khác nhau. Vì vậy, Samuele Pedroni lần đầu tiên phát hiện ra sự không nhất quán và giới thiệu thuật toán tuyến tính hóa C3. Nó được sử dụng để loại bỏ sự không nhất quán được tạo bởi thuật toán DLR. Nó có một số hạn chế nhất định là: & nbsp; & nbsp;
  • Trẻ em đi trước cha mẹ của chúng
  • Nếu một lớp kế thừa từ nhiều lớp, chúng được giữ theo thứ tự được chỉ định trong bộ thuật của lớp cơ sở.

Thuật toán tuyến tính hóa C3 hoạt động trên ba quy tắc: & nbsp; & nbsp;
Methods for Method Resolution Order(MRO) of a class: 
To get the method resolution order of a class we can use either __mro__ attribute or mro() method. By using these methods we can display the order in which methods are resolved. For Example 
 

Python3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
1

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
2
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
3
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
4
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
5
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
6

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
7
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
9__
class ExampleSimMod(ExampleSim, PhysicsMod):
    print 'modified example sim'
1

Python tuân theo thứ tự tra cứu đầu tiên và do đó cuối cùng gọi phương thức từ lớp A. Bằng cách tuân theo thứ tự độ phân giải phương thức, thứ tự tra cứu như sau. tuân theo thứ tự đầu tiên để giải quyết các phương thức và thuộc tính. Vì vậy, trong ví dụ trên, nó thực hiện phương thức trong lớp B. & nbsp; & nbsp; & nbsp; thứ tự kiểu cũ và mới: & nbsp; trong phiên bản cũ của Python (2.1), chúng tôi buộc phải sử dụng các lớp kiểu cũ nhưng trong Python ( 3.x & 2.2) Chúng tôi bị ràng buộc chỉ sử dụng các lớp mới. Các lớp phong cách mới là những lớp mà cha mẹ đầu tiên thừa hưởng từ python root ’lớp đối tượng. & Nbsp; & nbsp;

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0 Physics3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
7
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
9__
class ExampleSimMod(ExampleSim, PhysicsMod):
    print 'modified example sim'
1

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0 Physics3

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
0 Physics7Physics8
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
6

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
7
class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class PhysicsMod(Physics):
    arg1 = 'modified arg1'
55

Thứ tự phân giải phương pháp (MRO) trong cả hai kiểu khai báo là khác nhau. Các lớp kiểu cũ sử dụng DLR hoặc độ sâu đầu tiên từ trái sang phải thuật toán trong khi các lớp kiểu mới sử dụng thuật toán tuyến tính hóa C3 để phân giải phương thức trong khi thực hiện nhiều kế thừa. Tìm kiếm vì nó cần giải quyết phương thức nào phải được gọi khi một người được gọi bởi một thể hiện. Như tên cho thấy, thứ tự độ phân giải phương thức sẽ tìm kiếm độ sâu đầu tiên, sau đó đi sang trái sang phải. Ví dụ: & nbsp; & nbsp;

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class PhysicsMod(Physics):
    arg1 = 'modified arg1'
60

class ExampleSimMod(ExampleSim):
    print 'modified example sim'
8
class PhysicsMod(Physics):
    arg1 = 'modified arg1'
62

Output:   
 

Constructor C

(, , , )

[, , , ]

Có quan trọng trong nhiều kế thừa?

Trong nhiều kế thừa, các phương thức được thực thi dựa trên thứ tự được chỉ định trong khi kế thừa các lớp.the methods are executed based on the order specified while inheriting the classes.

Di truyền phân cấp trong Python là gì?

Di truyền phân cấp Nếu nhiều lớp dẫn xuất được tạo ra từ cùng một cơ sở, loại thừa kế này được gọi là di truyền phân cấp.Trong trường hợp này, chúng tôi có hai lớp cơ sở với tư cách là lớp cha mẹ (cơ sở) cũng như hai lớp (có nguồn gốc).If multiple derived classes are created from the same base, this kind of Inheritance is known as hierarchical inheritance. In this instance, we have two base classes as a parent (base) class as well as two children (derived) classes.

Nhiều kế thừa có tốt trong Python không?

Kế thừa là cơ chế để đạt được khả năng tái sử dụng mã khi một lớp (lớp con) có thể rút ra các thuộc tính của lớp khác (lớp cha).Nó cũng cung cấp tính chuyển tiếp tức là.Nếu lớp C kế thừa từ P thì tất cả các lớp phụ của C cũng sẽ kế thừa từ P.. It also provides transitivity ie. if class C inherits from P then all the sub-classes of C would also inherit from P.

Những hạn chế của thừa kế trong Python là gì?

Nhược điểm của thừa kế trong Python..
Giảm tốc độ thực thi: tải nhiều lớp vì chúng phụ thuộc lẫn nhau ..
Các lớp được ghép nối chặt chẽ: Điều này có nghĩa là mặc dù các lớp cha có thể được thực thi độc lập, các lớp con không thể được thực thi mà không xác định các lớp cha mẹ của họ ..