tudor
tudor

Reputation: 43

basic Python(ic) inheritance question

Quick question about Python(ic) inheritence: what is the difference between the idiom posted here (super(ChildClass,self).method(args)) vs. the one on the Python docs site (ParentClass.method(self,[args]))? Is one more Pythonic than the other?

Upvotes: 1

Views: 271

Answers (1)

Mike Graham
Mike Graham

Reputation: 76683

Using super(ChildClass, self).method(args) allows you to walk the method resolution order and -- if everyone but the last parent uses super -- call every class in the hierarchy exactly once. (Not that super only works with new-style classes.)

Using ParentClass.method(self, args) calls one specific class. It does not work when involved in multiple inheritance.

This article provides some description of the issue and clarifies a lot of issues for some people. I don't agree with all of its conclusions, but it provides good examples and discussion.

Upvotes: 4

Related Questions