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'
1class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2class ExampleSimMod[ExampleSim]:
print 'modified example sim'
3 class ExampleSimMod[ExampleSim]:
print 'modified example sim'
4class ExampleSimMod[ExampleSim]:
print 'modified example sim'
5class ExampleSimMod[ExampleSim]:
print 'modified example sim'
6class ExampleSimMod[ExampleSim]:
print 'modified example sim'
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8class ExampleSimMod[ExampleSim]:
print 'modified example sim'
9__ class ExampleSimMod[ExampleSim, PhysicsMod]:
print 'modified example sim'
1class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 class PhysicsMod[Physics]:
arg1 = 'modified arg1'
07class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2class ExampleSimMod[ExampleSim]:
print 'modified example sim'
3 class ExampleSimMod[ExampleSim]:
print 'modified example sim'
4class ExampleSimMod[ExampleSim]:
print 'modified example sim'
5class ExampleSimMod[ExampleSim]:
print 'modified example sim'
6class ExampleSimMod[ExampleSim]:
print 'modified example sim'
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8class ExampleSimMod[ExampleSim]:
print 'modified example sim'
9__ In class B3
class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 class PhysicsMod[Physics]:
arg1 = 'modified arg1'
11In class B7
Output:
In class B
class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2class ExampleSimMod[ExampleSim]:
print 'modified example sim'
3 class PhysicsMod[Physics]:
arg1 = 'modified arg1'
50class ExampleSimMod[ExampleSim]:
print 'modified example sim'
5class ExampleSimMod[ExampleSim]:
print 'modified example sim'
6The 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'
1class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2class ExampleSimMod[ExampleSim]:
print 'modified example sim'
3 class ExampleSimMod[ExampleSim]:
print 'modified example sim'
4class ExampleSimMod[ExampleSim]:
print 'modified example sim'
5class ExampleSimMod[ExampleSim]:
print 'modified example sim'
6class ExampleSimMod[ExampleSim]:
print 'modified example sim'
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8class ExampleSimMod[ExampleSim]:
print 'modified example sim'
9__ class ExampleSimMod[ExampleSim, PhysicsMod]:
print 'modified example sim'
1class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 class ExampleSimMod[ExampleSim, PhysicsMod]:
print 'modified example sim'
3class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2class ExampleSimMod[ExampleSim]:
print 'modified example sim'
3 class ExampleSimMod[ExampleSim]:
print 'modified example sim'
4class ExampleSimMod[ExampleSim]:
print 'modified example sim'
5class ExampleSimMod[ExampleSim]:
print 'modified example sim'
6class ExampleSimMod[ExampleSim]:
print 'modified example sim'
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8class ExampleSimMod[ExampleSim]:
print 'modified example sim'
9__ class ExampleSimMod[ExampleSim, PhysicsMod]:
print 'modified example sim'
1class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 class ExampleSimMod[ExampleSim, PhysicsMod]:
print 'modified example sim'
3class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2class ExampleSimMod[ExampleSim]:
print 'modified example sim'
3 class ExampleSimMod[ExampleSim]:
print 'modified example sim'
4class ExampleSimMod[ExampleSim]:
print 'modified example sim'
5class ExampleSimMod[ExampleSim]:
print 'modified example sim'
6class ExampleSimMod[ExampleSim]:
print 'modified example sim'
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8class ExampleSimMod[ExampleSim]:
print 'modified example sim'
9__ class ExampleSimMod[ExampleSim, PhysicsMod]:
print 'modified example sim'
1class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 class ExampleSimMod[ExampleSim, PhysicsMod]:
print 'modified example sim'
3class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2Physics
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8class ExampleSimMod[ExampleSim]:
print 'modified example sim'
9__ In class B3
In class B7
0 class ExampleSimMod[ExampleSim]:
print 'modified example sim'
ExampleSim
3
In class B
class ExampleSimMod[ExampleSim]:
print 'modified example sim'
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8Physics
1class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 Physics
3Diamond inheritance and it looks as follows: class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 Physics
5Class 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 B4
In class B5
Physics
0class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2Physics
7Đầu ra: & nbsp; & nbsp;
class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2Physics
7Trong 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'
1class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2Physics
7Python 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'
2Physics
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 Physics
3class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2Physics
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 Physics
7Physics
8class ExampleSimMod[ExampleSim]:
print 'modified example sim'
6class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2Physics
7Thứ 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'
2Physics
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 class PhysicsMod[Physics]:
arg1 = 'modified arg1'
07C3 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:
0class ExampleSimMod[ExampleSim]: print 'modified example sim'
11class PhysicsMod[Physics]: arg1 = 'modified arg1'
0class ExampleSimMod[ExampleSim]: print 'modified example sim'
15class PhysicsMod[Physics]: arg1 = 'modified arg1'
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'
1class ExampleSimMod[ExampleSim]:
print 'modified example sim'
2class ExampleSimMod[ExampleSim]:
print 'modified example sim'
3 class ExampleSimMod[ExampleSim]:
print 'modified example sim'
4class ExampleSimMod[ExampleSim]:
print 'modified example sim'
5class ExampleSimMod[ExampleSim]:
print 'modified example sim'
6class ExampleSimMod[ExampleSim]:
print 'modified example sim'
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8class ExampleSimMod[ExampleSim]:
print 'modified example sim'
9__ class ExampleSimMod[ExampleSim, PhysicsMod]:
print 'modified example sim'
1Python 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 Physics
3class ExampleSimMod[ExampleSim]:
print 'modified example sim'
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8class ExampleSimMod[ExampleSim]:
print 'modified example sim'
9__ class ExampleSimMod[ExampleSim, PhysicsMod]:
print 'modified example sim'
1class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 Physics
3class ExampleSimMod[ExampleSim]:
print 'modified example sim'
0 Physics
7Physics
8class ExampleSimMod[ExampleSim]:
print 'modified example sim'
6class ExampleSimMod[ExampleSim]:
print 'modified example sim'
7class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8class PhysicsMod[Physics]:
arg1 = 'modified arg1'
55Thứ 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'
8class PhysicsMod[Physics]:
arg1 = 'modified arg1'
60class ExampleSimMod[ExampleSim]:
print 'modified example sim'
8class PhysicsMod[Physics]:
arg1 = 'modified arg1'
62Output:
Constructor C [, , , ] [, , , ]