OOP - Object Oriented Programming in Python
In this section will dive into objects. In everyday life, we interact with
a variety of different objects. Each object has distinct characteristics and act on specific
functions, but some of these might share common properties. Example: Assume
a vehicles' company named VeComp.
Each vehicle can be considered as a unique object with
characteristics (horse power, color, model, brand etc). Their functions vary.
Some vehicles were manufactured for specific usage: like moving other cars or cleaning roads.
However, some of these have common characteristics. Some brands manufacture vehicles with similar
colors or models with same horse power. This section will have no quiz. Just exercises for you to get
familiar with classes.
Basic Syntax - Creating our first class
def functionOne(self, var1, ...):
object1 = ClassName()
object2 = ClassName()
First of all you need to define a name for your class. After that
you can declare some variables (a.k.a. characteristics if you want) and then
you can create some functions (if you want) for your objects. The last two lines
represent the 'birth' of the objects pointing class ClassName(). Let create our class.
>>>   class myClass:
def insertPerson(self, Name, Height, Email):
self.Name = Name
self.Height = Height
self.email = Email
>>>   person = myClass()
>>>   person.insertPerson( 'Raz', '185cm', 'firstname.lastname@example.org' )
- myClass is the name of the class. With every object creation
we reference the object to the class name. It can be any name you can think of. Simple?
- insertPerson() is a function which holds the properties of the person (object).
The self indicates the object itself. Name, Height and Email are variables declared
to the object.
- self.Name etc. Since self indication represents the object itself, it's quite
obvious that the Name variable we've declared will become "object's.Name" or more accurately,
"self.Name". Same of self.Height and self.Email.
- showNameAndHeight() is again a function which holds object's properties and its
main role is to display or print some characteristics belonged to the object.
- person = myClass() is the object we create in order to use the class.
Without an object, the class is useless. *Imagine having car's technical information without
having the car to actually drive it.*
- person.inserPerson() Now that we have the object to interact with the class, we
just gave this object a purpose. Properties. We gave it a Name, Height and Email.
- person.showNameAndHeight() will display self's or person's Name and Height to
the screen! It wasn't so bad, was it?