Questions tagged [method-resolution-order]

In object-oriented programming languages that support multiple inheritance, the method resolution order (MRO) is the order in which parent classes are searched during dynamic method resolution.

In object-oriented programming languages that support multiple inheritance, the method resolution order (MRO) is the order in which parent classes are searched during dynamic method resolution.

191 questions
198
votes
3 answers

TypeError: Cannot create a consistent method resolution order (MRO)

This is the code which I plan to use for my game, but it complains about an MRO error: class Player: pass class Enemy(Player): pass class GameObject(Player, Enemy): pass g = GameObject()
user188276
181
votes
5 answers

What does "mro()" do?

What does mro() do? Example from django.utils.functional: for t in type(res).mro(): # <----- this if t in self.__dispatch: return self.__dispatch[t][funcname](res, *args, **kw)
zjm1126
  • 63,397
  • 81
  • 173
  • 221
111
votes
4 answers

Method Resolution Order (MRO) in new-style classes?

In the book Python in a Nutshell (2nd Edition) there is an example which uses old style classes to demonstrate how methods are resolved in classic resolution order and how is it different with the new order. I tried the same example by rewriting…
sateesh
  • 27,947
  • 7
  • 36
  • 45
67
votes
1 answer

How does the order of mixins affect the derived class?

Say, I have the following mixins that overlaps with each other by touching dispatch(): class FooMixin(object): def dispatch(self, *args, **kwargs): # perform check A ... return super(FooMixin, self).dispatch(*args,…
41
votes
2 answers

What's the difference between super() and Parent class name?

Is there a difference between using super() and using the parent class name directly? For example: class Parent: def __init__(self): print("In parent") self.__a=10 class Child(Parent): def __init__(self): …
20
votes
9 answers

Change python mro at runtime

I've found myself in an unusual situation where I need to change the MRO of a class at runtime. The code: class A(object): def __init__(self): print self.__class__ print "__init__ A" self.hello() def hello(self): …
dilbert
  • 3,008
  • 1
  • 25
  • 34
18
votes
2 answers

Python self and super in multiple inheritance

In Raymond Hettinger's talk "Super considered super speak" at PyCon 2015 he explains the advantages of using super in Python in multiple inheritance context. This is one of the examples that Raymond used during his talk: class DoughFactory(object): …
Marc Tudurí
  • 1,869
  • 3
  • 18
  • 22
18
votes
2 answers

Why does __mro__ not show up in dir(MyClass)?

class MyClass(object): pass print MyClass.__mro__ print dir(MyClass) Output: (, ) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__',…
tur1ng
  • 3,139
  • 5
  • 24
  • 31
15
votes
1 answer

Python 3 builtin types __init__ doesn't call super().__init__?

When deriving from a builtin type as well as from some other class, it seems that the builtin type's constructor doesn't call the super class constructor. This results in __init__ methods not being called for types that come after the builtin in the…
15
votes
3 answers

Achieving multiple inheritance using python dataclasses

I'm trying to use the new python dataclasses to create some mix-in classes (already as I write this I think it sounds like a rash idea), and I'm having some issues. Behold the example below: from dataclasses import dataclass @dataclass class…
12
votes
2 answers

How does a Perl 6 object find a multi method that might be in a parent class or role?

Consider this example where a subclass has a multi method with no signature and one with a slurpy parameter: class Foo { multi method do-it { put "Default" } multi method do-it ( Int $n ) { put "Int method" } multi method do-it ( Str $s…
brian d foy
  • 129,424
  • 31
  • 207
  • 592
12
votes
3 answers

How do I dynamically add mixins as base classes without getting MRO errors?

Say I have a class A, B and C. Class A and B are both mixin classes for Class C. class A( object ): pass class B( object ): pass class C( object, A, B ): pass This will not work when instantiating class C. I would have to remove object…
Daan Timmer
  • 14,771
  • 6
  • 34
  • 66
11
votes
1 answer

Why can't I access .__mro__ attribute here?

Example from Raymond Hettinger's recepie class Root(object): def draw(self): # the delegation chain stops here assert not hasattr(super(Root, self), 'draw') class Shape(Root): def __init__(self, shapename, **kwds): …
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208
11
votes
1 answer

Why are classes being ordered this way in the MRO?

I have a problem with the Python MRO For this code: class F: pass class G: pass class H: pass class E(G,H): pass class D(E,F): pass class C(E,G): pass class B(C,H): pass class A(D,B,E): pass print(A.__mro__) I get this output: (
11
votes
1 answer

Does Python's MRO, C3 linearization work depth-first? Empirically it does not

I was reading Python Multiple Inheritance (on Programiz) and then I found this StackOverflow question, Method Resolution Order (MRO) in new-style classes? but in this question some programmers like Alex Martelli said it uses the depth-first…
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
1
2 3
12 13