Thursday 23 August 2018

Learn Basic OOP in Python

Completing the Basic Python Series before, this time we will learn OOP in Python. Same As with OOP in other programming languages, OOP is of course we will discuss

Class
Object
Constructor
Method
etc.
Writing (syntax)

class ClassName:
    
        # ....
        # ....
or

Employee class (object):

In python we can make a Class Description / Documentation that we make in String form

Employee class:
    'This Class is for Managing Employees'
    
# access outside Class
print employee .__ doc__

#output: This Class to Manage Employees


Class, Object, Constructor and Method
Consider the following example:

Employee class (object):

    'This class is for Employee data'
    jml_karyawan = 0 #Class variable
    
    #constructor
    def __init __ (self, kid, name, position):
        
        self.kid = kid
        self.name = name
        self. position = position
        Employee.jml_ employee + = 1
    
    # method
    def infoKaryawan (self):
    
        print "New employee enters"
        print "==================="
        print "ID:% s"% self.kid
        print "Name:% s"% self.nama
        print "Position:% s"% self. position


# how to access / use classes / create Objects

obj = Employee ("K001", "Reward", "Technician")
obj.infoKaryawan ()

# Add new employees
obj2 = Employee ("K002", "Nadya", "Accounting")
obj2.infoKaryawan ()

# Show total employees
print "-----------------------------"
print "Total Employees:% d"% Employees.jml_karyawan
The program above the output is

New employee enters
===================
ID: K001
Name: Ganjar
Position: Technician
New employee enters
===================
ID: K002
Name: Nadya
Position: Accounting
-----------------------------
Total Employees: 2

We are operating the one code above

__init__, what is that? __init__ is a constructor. The first method will be executed when we first create an Instance from Class. As in other languages ​​we can add Arguments to __init__. The Instance of Class will be initialized in __init__ and we can call it anywhere in that class.

Self, what is that? self is the Instance Class used to access Instance

  self.kid = kid
  self.name = name
  self. position = position
can also call methods in other methods

def method1 (self, argument):
   print "% s argument"% arg

def method2 ():
    self.method1 ("Argument")
Other programming languages ​​also have self or this. the self parameter is written first in the Argument method

def method (self, arg1, arg2, arg3 ....)


The way to classify Classes is generally the same as other Languages

obj = Employee ("K001", "Reward", "Technician")
obj.infoKaryawan ()


CLass variable? note the variable employee. we can create and define a variable in Class. and can be accessed anywhere either in the Class or outside the Class by Syntax

ClassName.variable_name
# Show total employees
print "-----------------------------"
print "Total Employees:% d"% Employees.jml_karyawan


Keep in mind that Python is not like OOP like other languages. therefore we do not find: private, protected and public. The reasons are many and honestly it is difficult to explain it. what is clear Python makers and developers have agreed that they don't need to add that functionality.

If you study deeper for what purpose, for such spaces, it is suitable for systems like what Python is. Maybe we will know more about the reason.



So brief explanation of OOP Python. there are still many that have not been explained from OOP material in python. But of course it will be discussed in the next OOP Python article.

Share this


0 Comments