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

class ClassName:

    def functionOne(self, var1, ...):
        ....
    def functionTwo(self):
        ....
    def functionThree(self):
        ....
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

        def showNameAndHeight(self):
            print self.Name
            print self.Height

>>>   person = myClass()
>>>   person.insertPerson( 'Raz', '185cm', 'raz@mail.com' )
>>>   person.showNameAndHeight()
'Raz'
'185cm'

Explanation:
  • 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?

exercise-icon Try it!

Create a class named Staff. Afterwards develop you first function named salaryData() with parameters self, salary, hours. In this function, all the data will be stored in self.salary, self.hours for initialization to the previous example. Create a second function named salaryCalc() with a single parameter the object or self. In salaryCalc() return the current operation: (self.salary * 12) + 300.



?

exercise-icon Try it!

Create an object named person for the Staff class we've made.
?

exercise-icon Try it!

Use object person to call the first function of the class with values 200 (hours) and 15 (salary) respectively.
?

exercise-icon Try it!

Use the same steps to call the second function of the class.
?

SubClasses & SuperClasses - Practice

Classes/OOP can be tricky if not learned properly. You need to practice a lot in order to get comfortable with what's going on, or how the data is passed into the class. There are types of classes named SubClasses and SuperClasses. Recall when we've talk about inheritance. Suppose we have class Alpha with some variables/properties. For some reason we have to declare another class as a child class with name Beta, which will SHARE & USE (inherits) Alpha's characteristics.

>>>   class Alpha:
          salary = 20000
          loan = 3000
>>>   class Beta(Alpha):
          pass   Means 'continue' doing nothing

>>>   person = Beta()   Creating object for child class Beta
>>>   person.loan   Accessing Alpha's class variable
3000

Keep practising object oriented scenarios. You can apply them later for developing your own graphical user interface (GUI) applications. GUI, it's all about objects, functions & events. If you can't understand how classes work, go back and practice more. Classes are very practical and can be used for anything. Don't forget to consult your documentation.
python.org | Contact Me | © ksoftware.eu 2016