Introduction To Python - Learn Python the hard way v2.0
voteup image
0

I understand that the following way of invoking a superclass constructor may be the preferred way. However, it does not work on all versions of Python. Specifically version 2.6

Is this a Python 3.0 feature ?

super(Manager, self).__init__(name, salary)

asked Dec 8, 2011

http://twitter.com/coding_insights's image

http://twitter.com/coding_insights

2 Answers:
voteup image
1

The act of calling super() from a subclass only works if the original class was extended from the Object class.

In other words:

class Song(object):
    def __init__(self, name, type):
        self.name = name
        self.type = type

class RapSong(Song):
    def __init__(self, name):
        super(RapSong, self).__init__(name, "Rap")

Here's more information about what I mean: http://stackoverflow.com/questions/805066/

answered Dec 16, 2011

http://twitter.com/mikegreenberg's image

http://twitter.com/mikegreenberg

voteup image
0

Thanks Mike, that worked. Though it did surprise me. It's almost like a gotcha... that by default object is the superclass of everything, even if it's not mentioned, but when we want to use the super keyword, then we need to explicitly extend object.

answered Dec 17, 2011

http://twitter.com/coding_insights's image

http://twitter.com/coding_insights

Provide an answer to this question.
This editor supports the markdown format. Please visit this page, If you are not familiar with Markdown.

Recent Course Updates: