0

This code I found somewhere in the Internet:

class Superclass(object):
    def __init__(self):
        print ('SuperClass: Do something')

class Subclass(Superclass):
    def __init__(self):
        super(Subclass, self).__init__()
        print ('SubClass: Do something else')

test = Subclass()

So, 'object' in 'class Superclass(object)' is unnecessary in my opinion. Well, I can remove it, and the program works fine as well.

The same we can see for example in Python's Observer pattern here:

http://en.wikipedia.org/wiki/Observer_pattern

Could you comment on what this 'object' is and what it is here for.

Michael
  • 3,703
  • 3
  • 28
  • 53

1 Answers1

1

In Python2, declaring a class is derived from object is necessary to make the class a new-style class. Otherwise, it is a classic class. super only works properly with new-style classes, so in Python2, Superclass must derive from object for the super call in Subclass to work.

In Python3, all classes are new-style. So using object is unnecessary.

Community
  • 1
  • 1
unutbu
  • 711,858
  • 148
  • 1,594
  • 1,547