Welcome to CCP
Hello, you
Lets get started !
OOP refers to Object Oriented Programming
It is the method Programming using classes and its instances
This another way of reusing code
So, we left of with basic class definitions, defining methods and the special __init__ method,
Have you ever wondered what if we try to print type of any custom made class or just directly print it,
Lets do it here:
here,
when printed type of tiger object, it gave us the class name and the weird name __main__ means the name of script, as we were in interpreter,
but if you go in an editor and right this same code and save it as jungle.py, then it would give <class 'jungle.Animal'>
and if you try to print directly the Animal object tiger, it would give the class name and script name as dot notation and then give the memory address of computer where the variable tiger is stored and is a Animal object,
I hope you understood it,
Now, lets talk about a new special method which helps to print objects or instances of classes in decent way rather than just printing its memory address,
the method (as it is always placed in classes) is __str__
it can be used like this:
here,
we wanted to just print the name of that object when we want to print that object through return keyword,
to recall,
return keyword is commonly used keyword which returns assigns a value or just assigns the value to the whole function,
for example:
Remember it ?
Look,
We don't need to memorize all of this syntax, the important in programming is the LOGIC the logic you use to solve a problem,
These programming languages are made to ease the process of integration of machines and humans rather a headache,
Nothing is impossible,
with this said lets move on to the next Exercise !
Exercise problem:
You have to make a point system with help of classes, properties and methods
Solution: (Don't dare to just cheat, try to understand it)
here you go:
import os; os.system("clear")
class Point: # There is a convention to start class names with capitals
def __init__(self, x, y): # using a shorthand property to omit the declaration part for variables, in this case: x & y
self.x = x
self.y = y
def calculate_distance(self, other):
# recall the distance formula
distance = ((self.x - other.x)**2 + (self.y - other.y)**2)**(1/2)
return distance
def __str__(self):
return "Point at ({}, {})".format(self.x, self.y)
# Till here beginner, intermediaters can go forward if they want to
# Note: if you want to see a more complex version then you can read after this comment, else: you need not to read after this
def construct_square(self, side):
point1 = Point(self.x + side, self.y)
point2 = Point(self.x, self.y - side)
point3 = Point(point1.x, point2.y)
point = self
return point, point1, point3, point2 # returned points in a clockwise manner
@classmethod # dont worry about this @, we will learn all of this
def display_square(cls, a, b, c, d): # where a, b, c and d are points of square
print("@" + "--"*abs(abs(b.x) - abs(a.x)) + "@") # abs() function returns absolute value of a number ignoring the positive or negative sign
for i in range(abs(abs(b.x) - abs(a.x))):
print("|" + " "*abs(abs(b.x) - abs(a.x))*2 + "|")
print("@" + "--"*abs(abs(b.x) - abs(a.x)) + "@")
point = Point(0, 1)
point1 = Point(1, -1)
distance = point.calculate_distance(point1)
distance = round(distance, 3) # Round off distance variable returned by the method to the 3rd decimal place
print("The distance of {} from {} is: {}".format(point, point1, distance))
print("\nThis is the square formed")
a, b, c, d = point.construct_square(10)
Point.display_square(a, b, c, d)
and it gives output as:
So, this was the fun exercise,
Now, lets move on to a shorthand trick used in python3 with the __init__ method:
We can here clearly see that,
we didn't declare name = "" or age = 0 but directly started to define name and age as self.name and self.age,
and also,
when we tried to print the name and age of that object, it printed us the name and age of the declared object,
From here,
I give a rest to the class,
you.take_rest()
I.catch_you_later() :)
Comments
Post a Comment