class Piece:
def __init__(self, color) -> None:
self.color = color
def move(self):
pass
class Knight(Piece):
def __init__(self, color) -> None:
super().__init__(color)
self.shape = 'horse'
def move(self):
print("Move in L ")
if __name__ == "__main__":
knight1 = Knight('white')
knight1.move()
print(knight1.color)
Lets try to create a class Point
import math
class Point:
"""
Represents the two-dimensional geometric co-ordinate
>>> p_0 = Point()
>>> p_1 = Point(3,5)
"""
def __init__(self, x: float = 0, y:float =0) -> None:
self.x = x
self.y = y
def reset(self) -> None:
"""
This instance method resets the geometric co-ordinates back to origin
"""
self.x = 0
self.y = 0
def distance(self, other: "Point") -> float:
"""
Calculate the Euclidean distance between two points
"""
#math.hypot(self.x - other.x, self.y-other.y)
return math.sqrt((self.x - other.x)**2 + (self.y -other.y)**2)
def move(self, x: float, y: float) -> None:
"""
This method moves the point to the new location in 2D space
"""
self.x += x
self.y += y
def print_location(self) -> str:
return f"({self.x}, {self.y})"
Lets use this point class
from point import Point
if __name__ == "__main__":
p1 = Point(0,0)
print(p1.print_location())
p1.move(1,2)
print(p1.print_location())
p2 = Point(10,21)
print(f"distance between p1 and p2 is {p2.distance(p1)}")
from point import Point, Point3D
if __name__ == "__main__":
p13d = Point3D(0,0,0)
p13d.move(1,2,3)
print(p13d.print_location())
p23d = Point3D(4,5,6)
print(f"distance between p13d and p23d is {p23d.distance(p13d)}")
Technically all the python classes are sub-classes of the special built in class named object