Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Welcome to Learning Python 101.

Today we are
going to learn a new concept of programming known as
Object Oriented Programming.Let’s get started.

Generalized understanding: What is Object oriented programming? Well, till now we have
been programming in a way which is called procedural programming.It’s a way in which we
divide a program into a set of functions, we declare variables and keep data inside them. Now,
we have data stored in a bunch of variables, and functions that operate on the data.
This style of programming is very simple and straightforward.
However, As your program grows, it will end up with a bunch of functions, that are all over the
place. you might find yourself copying and pasting lines of code over and over.
you make a change to one function and then several other functions break.
There is so much interdependence between all these functions, it becomes problematic.
Object Oriented Programming came to solve this problem. in oop we combine a group of related
variables and functions into a unit.We call that unit an object. We refer to these variables as
properties and the functions as methods.The way in which all of these objects will behave is
called a class.let’s see an example,

Think of a car. A car is an object with properties such as make,model and color also it contains
methods like start,stop and move.

Video link - Object-oriented Programming in 7 minutes | Mosh - YouTube

Classes

Classes are like blueprints, Every object that is created belongs to a class.As we need a
blueprint to build some objects.These objects obey the class properties and methods. The
syntax of Creating a class is -

Class Class_Name:
Declare variables
Declare functions

Class is a keyword followed by the class name. Then we declare some variables and functions.
These functions will use those variables and perform particular tasks.All objects created from
this class will have the same functionalities..

Class Person:
x=5
Person p1
print(p1.x)
Output- 5
Here we created a class named person and gave a variable x, which stores 5.Then we created
an object named p1 which is of class person. Finally we printed the variable of that object.Now,
we can also add functions to it and call the function using that object. Notice that we access the
properties and methods using a dot sign. Now, if we want to create an object and initialize its
properties upon creation, then to do that we need to write a __init__ function. This is called a
constructor and it is called the moment an object is created. The syntax is

def __init__(self):
Self.variable_name = values

Let’s see an example to understand better

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

Output-
John
36

Here we created an object of class person. Now as soon as we create that object, the init
function gets called. We pass john and 36 as parameters which are then stored into the classes
variable name and age respectively.In the programs,
We have seen this Self word a lot. What does it mean?
The self parameter is a reference to the current instance of the class, and is used to access
variables that belong to the class.

It does not have to be named self , you can call it whatever you like, but it has to be the first
parameter of any function in the class. it’s better to just keep the name self. Let’s declare a
function and call it from an object

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
Output:-
Hello my name is John
As we can see the first parameter of myfun is self.We also called the function from the object
that we created named p1. Again accessing any variable or function of an object is done by
Object_name.variable/function name
Object_name.variable/function name
We can also change values of properties by just doing this
p1.age = 40
print(p1.age)
Output:-
40

Inheritance

Generalized Understanding: What does the word “inherit” mean? Well, Inherit means
someone has access to another person’s properties legally. Same goes in terms of our
code.When we have multiple classes. Then if we say something like Class A has
inherited Class B. That means, A has access and can use the properties of Class
B.Here, Class A is known as child class while Class B is known as parent class.

Any class can be a parent class. So the syntax of a parent class is just declaring
classes.However, In order to declare a child class we will have to name the parent class
in a parentheses when we write the child class.Something like this

class Child_class_name(Parent_class_name):
#properties

Let’s look at an coding example

class Person:
firstname = "john"
lastname = "doe"
def printname(self):
print(self.firstname, self.lastname)
class Child(Person):
pass
x = Child()
x.printname()

Here, as we can see we haven’t written any code in the child class.Notice that we
added a pass keyword.That’s because if we want to create an empty class we will have
to add that pass keyword.Going back, we created a child class which inherited Person
class. Although there is no printname function or any variables the line x.printname() still
printed the names John Doe.Why?! Because, the child class has access to the parent
class properties and methods and so we were able to use them rather than writing the
same code again.This is why inheritance is so important. It enables us to reuse the
codes that we have already written.

we didn’t write any constructor.If we write constructors in both classes what do you think
will happen? Well in that case the child class init function will override the parent
classes function.As such the child classes init function will get executed.

class Person:
def __init__(self):
self.firsname = "John"
print(self.firstname)
class Child(Person):
def __init__(self):
self.firstname = "Albert"
print(self.firstname)
x = Child()
Output : -
Albert

As we can see here, the object, called the child class constructor and the output is
albert.If we want to call the parent classes constructor then simply call the parent class
constructor from inside the child class constructor.

class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)

Person is the parent class

There is also a keyword called super(). Which makes the child class inherit all the
methods and properties from its parent.By using the super() function, you do not have to
use the name of the parent element, it will automatically inherit the methods and
properties from its parent.

super()

class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year

def welcome(self):
print("Welcome", self.firstname, self.lastname, "to the class of",
self.graduationyear)

x = Student("Mike", "Olsen", 2019)


x.welcome()
Output:-
Welcome Mike Olsen to the class of 2019

Let’s do a walkthrough of this code. At first when we create a student class object x it
will receive a call to the constructor of student. Inside it as we see a super function call it
goes to the parent class constructor or __init__ function.Which creates the variables
firstname and second name. The values mike and olsen gets assigned to them.Then
the child class property graduationyear is initialized to the year 2019.
Finally calling the welcome method prints the output.

Well that was it for today.With this you are almost done with this crash course.There is
only a few topics left like error handling,files,importing libraries.But now it’s safe to
assume that you can code properly.We will also build a project and get a few more
details.Bye bye for now, Hope to see you guys soon..

You might also like