Python super() inheritance and needed arguments -


considering:

class parent(object):      def altered(self):         print "parent altered()"  class child(parent):      def altered(self):         print "child, before parent altered()"         super(child, self).altered()    # arguments needed?  why child , self?         print "child, after parent altered()" 

in python 2.7, why must child passed argument super() call? exact intricacies of using super instead of letting work.

super figures out next class in method resolution order. 2 arguments pass in lets figure out - self gives entire mro via attribute; current class tells along mro right now. super doing basically:

def super(cls, inst):     mro = inst.__class__.mro() # derived class     return mro[mro.index(cls) + 1] 

the reason current class rather base class because entire point of having super have function works out base class rather having refer explicitly - can cause problems if base class' name changes, if don't know parent class called (think of factory functions namedtuple spit out new class), , in multi-inheritance situations (where next class in mro mightn't 1 of current class' bases).


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -