Python: How do I make a subclass from a superclass?


Question

In Python, how do you make a subclass from a superclass?

1
78
10/22/2009 2:33:51 PM

# Initialize using Parent
#
class MySubClass(MySuperClass):
    def __init__(self):
        MySuperClass.__init__(self)

Or, even better, the use of Python's built-in function, super() (see the Python 2/Python 3 documentation for it) may be a slightly better method of calling the parent for initialization:

# Better initialize using Parent (less redundant).
#
class MySubClassBetter(MySuperClass):
    def __init__(self):
        super(MySubClassBetter, self).__init__()

Or, same exact thing as just above, except using the zero argument form of super(), which only works inside a class definition:

class MySubClassBetter(MySuperClass):
    def __init__(self):
        super().__init__()
82
11/16/2018 2:20:35 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon