Misspelled the method you invoke via super() in a Python method? It’s a runtime error: We get: AttributeError: ‘super’ object has no attribute ‘y’
A Python method can invoke a method in the parent class with super(): super() doesn’t invoke the method. It’s a proxy on which you invoke the method you want.
If a Python subclass defines a method with the same name as the parent, the parent’s method isn’t invoked:
Thanks to ICPO/MRO, if a child Python class doesn’t define a method, the parent method will be invoked:
Inheritance in Python changes the attribute lookup path, aka the “method resolution order,” or MRO: Child.__mro__ # (__main__.Child, object) Child.__mro__ # (__main__.Child, __main__.Parent, object)
Set one Python class to inherit from another by putting the parent in (): Child.__bases__ # who does Child inherit from?
Python uses attributes where other languages use different terms (and data structures): – Instance variable — attribute on an instance (self)– Class variable — attribute on a class– Method — callable (function) attribute on a class
Python methods are stored on a class, not an instance. Given x.m(), how does Python find it? The ICPO rule: – First, look for an attribute on the _i_nstance.– Not there? Look on the _c_lass.– Not here? Look on the _p_arent.– Not there? Look on _o_bject, the final parent.
By default, every Python class inherits from “object”. You can see this by checking the __bases__ attribute on a class: __bases__ is a tuple — hinting (correctly) that Python supports multiple inheritance.
Creating a Python class? You’re already using composition, the has-a relationship. How? When you assign to an attribute (e.g., self.name = name) in a Person class, you’re saying, “Person has-a name”. name could be a builtin str, or an instance of a custom PersonName class.