To begin, we must first understand what Object-oriented programming (OOP) really is. OOP is a programming language model which revolves around objects instead of actions. It also revolves around data instead of logic. OOP is based on the fact that we care about the objects we want to manipulate instead of the logic required to manipulate them.
Some of the basic concepts of OOP are classes, instance variables, inheritance, instance, method and object.
Class: A class is like a blueprint that helps to describe how to make something.
Instance Variables: An instance variable is a variable defined inside a method in a class and only belongs to the instance of the current class.
Inheritance: Inheritance is transfer of characteristics of one class (super-class) to another class (sub-class) which allows the subclass to use the methods from the super-class.
Instance: An instance is an object of a class.
Method: A method is a kind of function defined inside the class
Object: An object is a data structure defined by its class. The object comprises of class variables, instance variables and methods of the class.
Now that the basic terminology has been explained, it is time to actually go through an example of a class (docstrings have been omitted)
1. class Point:
2.
3. def __init__(self, coord):
4. self.coord = [float(x) for x in coord]
5.
6. def distance_to_origin(self):
7. return (sum([x**2 for x in self.coord]))**(1/2)
8.
9. def __eq__(self, other):
10. return (isinstance(other, Point) and self.coord == other.coord)
This is an example of a simple class called Point which takes in coordinates in any dimension and has a method that returns the distance to the origin of the coordinate given.
Now we can go through each line of code explaining how OOP works.
Firstly, on line 1, the class is declared using the keyword class followed by the name Point and a colon.
Next, line 3 contains the special method called __init()__ which is a class initialization method that is called by Python when an instance of the class is created. This method is declared with two underscores before the name and two underscores after the parentheses. Inside the parentheses are the parameters however the difference in OOP is that you must have the first argument in every method called "self". On line 4, in the __init()__ method, a new instance variable is defined called coord which is preceded by the "self" keyword and then a period.
Line 6 is another method declaration, and line 7 returns the distance from the origin to the coordinates passed by the user.
Line 9 is an example of another special method in Python OOP which is also declared by two underscores before the name and two after the parentheses. This example is the eq method which checks if two instances are equal to one another. In this case, on line 10, this method checks if "other" which is a keyword to define the second instance is actually an instance of the class Point and if the coord's passed in are equal.
To declare the class shown above, you can do:
point1 = Point([3.0, 4.0]).
This object creates an instance of class Point and now the methods in the class can be called using point1. One key thing to note is that in the declaration of __init()__ there are two parameters, "self" and "coord" however only one passed in the object we just created. This is because "self" is a special parameter which users don't need to include themselves.
Now from this object, we can access the other method in the class by doing point1.distance_to_origin() and this would return the float "5.0".
To find out how to use the final special method __eq__ we can create another object called:
point2 = Point([3.0, 5.0]).
Now you can write "point1 == point2" in Pythons constructor and this would call the __eq__ method in the class. It would return False because even though point2 is an instance of class Point, the coord passed in is not the same.
Therefore, as shown above, these are the basic concepts in Python OOP and using the basic terminology and an example, I have shown how to write an example of a class, declare it and use it in Python.