Last active
September 14, 2021 23:02
-
-
Save msyvr/7cead25abcea29ed767a455673748ac0 to your computer and use it in GitHub Desktop.
python: basic inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point: | |
def __init__(self, initX, initY): | |
self.x = initX | |
self.y = initY | |
def distanceFromOrigin(self): | |
return ((self.x ** 2) + (self.y ** 2)) ** 0.5 | |
def __str__(self): | |
return "x=" + str(self.x) + ", y=" + str(self.y) | |
p = Point(7, 6) | |
print(p) | |
#class LabeledPoint(Point): | |
# pass | |
class LabeledPoint(Point): | |
def __init__(self, initX, initY, label): | |
self.x = initX | |
self.y = initY | |
self.label = label | |
def __str__(self): | |
return "x=" + str(self.x) + ", y=" + str(self.y) + " (" + self.label + ")" | |
labeledPt = LabeledPoint(7,6,"Here") | |
print(labeledPt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment