Today I learned about classes in Python and did a little exercise which I thought would be helpful to understand the basic concepts.
So let us have a look at the code. In the file robots.py I define a Robot base class with some common properties and methods and 2 descendant classes RobotCar and RobotShip which have their own implementation. The base class manages a position which I wanted to be structured. So I created another class Position with x and y coordinates. I have tried to use Python best practices regarding naming conventions and code structure (like one line between methods, two lines between classes).
# robots.py class Position: def __init__(self, x: int, y: int): self.x = x self.y = y class Robot: def __init__(self, position: Position, name: str): self.position = position self.name = name def move_by(self, delta_x: int, delta_y: int): self.position.x += delta_x self.position.y += delta_y def show_position(self): print('X: {}'.format(self.position.x)) print('Y: {}'.format(self.position.y)) def show_name(self): print('I am: {}'.format(self.name)) class RobotCar(Robot): def __init__(self, position: Position, name: str, seats: int): super().__init__(position, name) self.seats = seats def show_seats(self): print('Number of seats: {}'.format(self.seats)) class RobotShip(Robot): def __init__(self, position: Position, name: str, displacement: float): super().__init__(position, name) self.displacement = displacement def show_displacement(self): print('Displacement of ship: {}'.format(self.displacement))
Let’s discuss the interesting parts:
Inheritance in Python
You inherit from a base class by defining the descendant class and putting the parent class in parenthesis.
class RobotCar(Robot):
Defining a constructor and call the constructor of the parent class
Inside the constructor of the descendant class you call the parents constructor by using super(). All properties and methods of the parent class will exist in the descendant classes.
def __init__(self, position: Position, name: str, seats: int): super().__init__(position, name) self.seats = seats
If you want to achieve different access levels of methods in a class hierarchy like protected or private in C# you can prefix your methods with one underscore for protected and two underscores for a private method.
def _protected_method(self): print("This is a protected method") def __private_method(self): print("This is a private method")
Running the robots
from robots import RobotCar, RobotShip, Position robot_car = RobotCar(Position(0, 0), 'dirtrunner', 2) robot_ship = RobotShip(Position(0, 0), 'speedboat', 34.5) robot_car.move_by(5,12) robot_car.show_name() robot_car.show_position() robot_car.show_seats() robot_ship.move_by(25,32) robot_ship.show_name() robot_ship.show_position() robot_ship.show_displacement()
Output
I am: dirtrunner
X: 5
Y: 12
Number of seats: 2
I am: speedboat
X: 25
Y: 32
Displacement of ship: 34.5
That’s it for a simple example of how the basics of Object Orientation works in Python. If you are like on the learning path create an example of your own and have fun experimenting with the code!
Leave a Reply