method-resolution-order

how does metaclass work with the MRO list when super() is called?

孤街浪徒 提交于 2019-12-06 14:28:43
I'm really confused by the following code sample: class Meta_1(type): def __call__(cls, *a, **kw): # line 1 print("entering Meta_1.__call__()") print(cls) # line 4 print(cls.mro()) # line 5 print(super(Meta_1, cls).__self__) # line 6 rv = super(Meta_1, cls).__call__(*a, **kw) # line 7 print("exiting Meta_1.__call__()") return rv class Car(object, metaclass=Meta_1): def __new__(cls, *a, **kw): print("Car.__new__()") rv = super(Car, cls).__new__(cls, *a, **kw) return rv def __init__(self, *a, **kw): print("Car.__init__()") super(Car,self).__init__(*a, **kw) if __name__ == '__main__': c = Car()

python3 super doesn't work with PyQt classes

白昼怎懂夜的黑 提交于 2019-12-05 05:29:36
There is a simple program in python3: from PyQt4 import QtCore import PyQt4 class Bar(object): def __init__(self): print("Bar start") super(Bar, self).__init__() print("Bar end") class FakeQObject(object): def __init__(self): print("FakeQObject start") super(FakeQObject, self).__init__() print("FakeQObject end") class Foo(QtCore.QObject, Bar): #class Foo(FakeQObject, Bar): def __init__(self): print("Foo start") super(Foo, self).__init__() print("Foo end") print(Foo.__mro__) print(PyQt4.QtCore.PYQT_VERSION_STR) f = Foo() a) When class Foo inherits from QtCore.QObject and Bar we get: (<class '_

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

北慕城南 提交于 2019-12-05 01:12:39
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 ) { put "Str method" } multi method do-it ( Rat $r ) { put "Rat method" } } class Bar is Foo { multi method do-it { put "Bar method" } multi method do-it (*@a) { put "Bar slurpy method" } } Foo.new.do-it: 1; Foo.new.do-it: 'Perl 6'; Foo.new.do-it: <1/137>; Foo.new.do-it; put '-' x 10; Bar.new.do-it: 1; Bar.new.do-it: 'Perl 6'; Bar.new.do-it: <1/137>; Bar.new.do

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

青春壹個敷衍的年華 提交于 2019-12-04 02:29:37
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 from class C to make it work. (Else you'll get MRO problems). TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases B, object, A However, my case is a bit more complicated. In my case class C is a server where A and B will be plugins that are loaded on startup. These are residing in their own

Python self and super in multiple inheritance

那年仲夏 提交于 2019-12-03 16:05:11
问题 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): def get_dough(self): return 'insecticide treated wheat dough' class Pizza(DoughFactory): def order_pizza(self, *toppings): print('Getting dough') dough = super().get_dough() print('Making pie with %s' % dough) for topping in toppings: print('Adding

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

不羁的心 提交于 2019-12-03 12:38:40
问题 class MyClass(object): pass print MyClass.__mro__ print dir(MyClass) Output: (<class '__main__.MyClass'>, <type 'object'>) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] Why is __mro__ not listed with dir() ? 回答1: From the Python documentation: Because dir() is supplied primarily as a

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

馋奶兔 提交于 2019-12-03 03:53:22
class MyClass(object): pass print MyClass.__mro__ print dir(MyClass) Output: (<class '__main__.MyClass'>, <type 'object'>) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] Why is __mro__ not listed with dir() ? From the Python documentation: Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries

Method Resolution Order in case of Base Classes Having different init params

我们两清 提交于 2019-12-02 00:45:31
问题 I am trying to understand MRO in Python. Although there are various posts here, I am not particularly getting what I want. Consider two classes A and B derived from BaseClass , each having an __init__ taking different params. class BaseClass(object): def __init__(self): print "I am the base class" class A(BaseClass): def __init__(self, something, anotherthing): super(A, self).__init__() self.something = something self.anotherthing = anotherthing def methodsA(self): ... class B(BaseClass): def

Method Resolution Order in case of Base Classes Having different init params

≯℡__Kan透↙ 提交于 2019-12-01 20:52:54
I am trying to understand MRO in Python. Although there are various posts here, I am not particularly getting what I want. Consider two classes A and B derived from BaseClass , each having an __init__ taking different params. class BaseClass(object): def __init__(self): print "I am the base class" class A(BaseClass): def __init__(self, something, anotherthing): super(A, self).__init__() self.something = something self.anotherthing = anotherthing def methodsA(self): ... class B(BaseClass): def __init__(self, someOtherThing): super(B, self).__init__() self.someOtherThing = someOtherThing def

How does Python's super() actually work, in the general case?

本秂侑毒 提交于 2019-12-01 06:20:12
问题 There are a lot of great resources on super() , including this great blog post that pops up a lot, as well as many questions on Stack Overflow. However I feel like they all stop short of explaining how it works in the most general case (with arbitrary inheritance graphs), as well as what is going on under the hood. Consider this basic example of diamond inheritance: class A(object): def foo(self): print 'A foo' class B(A): def foo(self): print 'B foo before' super(B, self).foo() print 'B foo