Everything in OOP is grouped as self sustainable "objects". The “hand” is a class. Your body has two objects of the type "hand", named "left hand" and "right hand". Their main functions are controlled or managed by a set of electrical signals sent through your shoulders (through an interface). So the shoulder is an interface that your body uses to interact with your hands. The hand is a well-architected class. The hand is being reused to create the left hand and the right hand by slightly changing the properties of it.
class is simply a representation of a type of object. It is the blueprint, or plan, or template, that describes the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.
class human:
def __init__(self, name, sex, weight):
self.name=name
self.sex=sex
self.weight=weight
__init__ is a special method, and it is called every time when a new human is created. In a new human, he has some attributes, name, sex, and weight. These attributes describe the human.
def __eq__(self, other):
return self.name==other.name and
self.sex==other.sex and
self.weight==other.weight
__eq__is also a special method, and it is used when we compare two human. It is common that when we compare two people, we want to compare their name, weight, height etc. These are all the attributes of the person. It works same way in the class Human. When we compare two human, we compare the attributes of the two human.
__str__ and __repr__are special methods. Both of the methods return str. __repr__ gives a str representation of the human. __str __gives an easily understandable str representation of the information stored in the class human.
No comments:
Post a Comment