Back
Close

Computing with Data

elgeish
33.5K views
Previous: Classes Next: The Empty Class

Inheritance

Class inheritance in Python is indicated by including the base class in parenthesis after the class name in the class definition:

class Point:
'''Represents a point in a 2-D coordinate space.'''
num_points = 0
def __init__(self, x, y):
Point.num_points += 1
self.x = x
self.y = y
def __del__(self):
Point.num_points -= 1
print("destructing a Point object")
print("{} points left".format(Point.num_points))
class NamedPoint(Point):
'''Represents a named point in a 2-D coordinate space.'''
num_points = 0
def __init__(self, x, y, name):
# call superclass constructor
super().__init__(x, y)
NamedPoint.num_points += 1
self.name = name
def __del__(self):
super().__del__()
NamedPoint.num_points -= 1
print("destructing a NamedPoint object")
print("{} named points left".format(NamedPoint.num_points))
np = NamedPoint(0, 0, "origin point")
print("number of named points:", NamedPoint.num_points)
print("x = {0.x}, y = {0.y}, name = {0.name}".format(np))
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants