super() in python

super(type[, object-or-type])   class.__mro__:  This attribute is a tuple of classes that are considered when looking for base classes during method resolution.

this can be used to get the method resolution order for both getattr() and super()

One pitfall about super() that I encountered: when using super() in a derived class , do not user super( self.__class__ ,self), which will result recursive loop in certain case (derived class has a child class too, and the child class is calling super() in the same method) the reason is self.__class__ will point to the instance class type.

this issue is also discussed here: http://stackoverflow.com/questions/18208683/when-calling-super-in-a-derived-class-can-i-pass-in-self-class

self.__class__ might not be a subclass, but rather a grandchild-or-younger class, leading to a stack-breaking loop.”   Another use case for super() is in multiple inheritance case: http://www.artima.com/weblogs/viewpost.jsp?thread=236275

Another great article about how to use super() in python correctly:

https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

http://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s