Re: Python's method-resolution algorithm: An implementation question
Thanks! On Tue, Aug 15, 2017 at 10:41 PM, Ian Kellywrote: > > On Tue, Aug 15, 2017 at 12:57 PM, Evan Aad wrote: > > I don't see how, since the L(B*)'s are listed in order in the argument > > list: L(B1), L(B2), ..., and each L(B*) starts with B*: L(B1) = > ...>, L(B2) = , ... > > > > Could you please give a counter-example? > > Sure. > > merge( , ) -> > > vs: > > merge( , , ) -> TypeError > > Or in Python: > > >>> class A1: pass > >>> class A2: pass > >>> class B1(A1): pass > >>> class B2(A2, B1): pass > >>> class C(B1, B2): pass > Traceback (most recent call last): > File "", line 1, in > TypeError: Cannot create a consistent method resolution order (MRO) > for bases B1, B2 > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Python's method-resolution algorithm: An implementation question
On Tue, Aug 15, 2017 at 12:57 PM, Evan Aadwrote: > I don't see how, since the L(B*)'s are listed in order in the argument > list: L(B1), L(B2), ..., and each L(B*) starts with B*: L(B1) = ...>, L(B2) = , ... > > Could you please give a counter-example? Sure. merge( , ) -> vs: merge( , , ) -> TypeError Or in Python: >>> class A1: pass >>> class A2: pass >>> class B1(A1): pass >>> class B2(A2, B1): pass >>> class C(B1, B2): pass Traceback (most recent call last): File "", line 1, in TypeError: Cannot create a consistent method resolution order (MRO) for bases B1, B2 -- https://mail.python.org/mailman/listinfo/python-list
Re: Python's method-resolution algorithm: An implementation question
I don't see how, since the L(B*)'s are listed in order in the argument list: L(B1), L(B2), ..., and each L(B*) starts with B*: L(B1) =, L(B2) = , ... Could you please give a counter-example? On Tue, Aug 15, 2017 at 9:44 PM, Ian Kelly wrote: > > On Tue, Aug 15, 2017 at 9:56 AM, Evan Aad wrote: > > According to the description of Python's method resolution order (mro) > > (https://www.python.org/download/releases/2.3/mro/), a.k.a. C3 > > linearization (see Wikipedia), the algorithm can be described as > > follows: > > > > "the linearization of C is the sum of C plus the merge of the > > linearizations of the parents and the list of the parents." > > > > Symbolically, the algorithm is defined recursively thus: > > > > L(O) = > > L(C) = + merge(L(B1),..., L(Bn), ) > > > > where > > > > * O is the class from which every class inherits. > > * C is a class that inherits directly from B1, ..., Bn, in this order. > > * < and > are list delimiters. > > * + is the list-concatenation operator. > > * 'merge' merges its list arguments into a single list with no > > repetitions in the manner described in the next paragraph. > > > > Consider the head of the first list, i.e L(B1)[0]: if it is a good > > head, i.e. if it is not in the proper tail of any of the other lists, > > add it to the linearization of C, and remove it from all the lists in > > the merge. Otherwise, consider the head of the next list, etc. Repeat > > until no more classes, or no good heads. In the latter case, it is > > impossible to construct the merge. > > > > > > > > Why is the list of parents given as an argument to > > 'merge'? Will the algorithm produce different results if this argument > > is omitted? > > It ensures that the parents are linearized in that order. Without that > list, it would be possible for B2 to appear before B1 in the MRO of C. > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Python's method-resolution algorithm: An implementation question
On Tue, Aug 15, 2017 at 9:56 AM, Evan Aadwrote: > According to the description of Python's method resolution order (mro) > (https://www.python.org/download/releases/2.3/mro/), a.k.a. C3 > linearization (see Wikipedia), the algorithm can be described as > follows: > > "the linearization of C is the sum of C plus the merge of the > linearizations of the parents and the list of the parents." > > Symbolically, the algorithm is defined recursively thus: > > L(O) = > L(C) = + merge(L(B1),..., L(Bn), ) > > where > > * O is the class from which every class inherits. > * C is a class that inherits directly from B1, ..., Bn, in this order. > * < and > are list delimiters. > * + is the list-concatenation operator. > * 'merge' merges its list arguments into a single list with no > repetitions in the manner described in the next paragraph. > > Consider the head of the first list, i.e L(B1)[0]: if it is a good > head, i.e. if it is not in the proper tail of any of the other lists, > add it to the linearization of C, and remove it from all the lists in > the merge. Otherwise, consider the head of the next list, etc. Repeat > until no more classes, or no good heads. In the latter case, it is > impossible to construct the merge. > > > > Why is the list of parents given as an argument to > 'merge'? Will the algorithm produce different results if this argument > is omitted? It ensures that the parents are linearized in that order. Without that list, it would be possible for B2 to appear before B1 in the MRO of C. -- https://mail.python.org/mailman/listinfo/python-list