class Employee:
def __init__(self, name, age, eid):
self.name = name
self.age = age
self.eid = eid
employeeObject = Employee('employeeName', 20, 1101)
print(employeeObject)
Outputs
<__main__.Employee object at 0x000001DBB695FB50>
This occurs because we did not code a behaviour for our objects when they are used in String Scenarios. We must override two functions: str() and repr().
To make an object printable.
We must override two built-in base functions in Python:
__str__()
__repr__()
The __str__() method returns a human-readable, or informal, string representation of an object. Often called when the object is within a print() or str()
The __repr__() method returns a more information-rich, or official, string representation of an object. This is often called when your custom object needs to be displayed within another or any other sitution where __str__() is not used.
Fixing the Employee classes
class Employee:
def __init__(self, name, age, eid):
self.name = name
self.age = age
self.eid = eid
def __str__(self):
return f"{self.eid}: {self.name}"
def __repr__(self):
return f'Employee(name = {self.name}, age = {self.age}, id = {self.eid})'
employeeObject = Employee('employeeName', 20, 1101)
print(employeeObject)
print(repr(employeeObject))
Outputs:
1101: employeeName
Employee(name = employeeName, age = 20, id = 1101)
Other Base Functions to Override.
Addition, Subtraction, Multiplication, Division
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other_point):
return Point(self.x + other_point.x, self.y + other_point.y)
def __str__(self):
return f"Point({self.x}, {self.y})"
p1 = Point(1,2)
p2 = Point(3,4)
p3 = p1 + p2 # This is possible due to the override of __add__()
print(p3) # Outputs Point(4,6)
The built-in arithmetic operators can be overridden to allow your custom class objects to interact with them. The subtraction, multiplication, and division operators will be overridden similar to the addition method above.
__eq__(self, other): Checks if two objects are equal (==).
__ne__(self, other): Checks if two objects are not equal (!=).
__lt__(self, other): Checks if the object is less than another (<).
__le__(self, other): Checks if the object is less than or equal to another (<=).
__gt__(self, other): Checks if the object is greater than another (>).
__ge__(self, other): Checks if the object is greater than or equal to another (>=).