Python Fundamental — Object Oriented Programming (OOP)

M. Rizky Maulana
2 min readApr 5, 2021

Finally this is the last chapter of Python Fundamental. Also, you can check my previous article :

Python has been an object-oriented language since the Python language itself was created. Creating and using classes and objects in Python is really easy. In this tutorial, you will be helped to become an expert in the use of Python object-oriented programming.

The term in OOP

Class = User-defined Prototype class for objects that define a set of attributes that characterize the object of any class. Attributes are member data (class variables and example variables) and methods, accessed via dot notation.

Class variable = A variable that is shared by all class instances. Class variables are defined within the class but outside the methods of any class. Class variables are not used as often as sample variables.

Data member = A class variable or example variable that stores data associated with the class and its objects.

Function overloading = Assigning more than one behavior to a specific function. The operations performed vary according to the type of object or argument involved.

Instance variable = A variable that is defined inside a method and belongs only to the current class instance.

Inheritance = The transfer of class characteristics to other classes derived from it.

Instances = Individual Object of a specific class. Object that belongs to class Circle, for example, is an instance of class Circle.

Instantiation = Creation of an instance of a class.

Method = A special type of function defined in the class definition.

Object = A unique example of a data structure defined by its class. An object consists of both data members (class variables and example variables) and methods.

Operator overloading = The assignment of more than one function to a particular operator.

Create Python Class

The class statement is used to create a new class definition. The class name immediately follows the class keyword followed by a colon as follows.

class ClassName: ‘Optional class documentation string’ class_suite

Below is an example of how to create a class and its use:

class Employee:
‘Common base class for all employees’
empCount = 0
def __init __ (self, name, salary):
self.name = name
self.salary = salary
Employee.empCount + = 1
def displayCount (self):
print “Total Employee% d”% Employee.empCount
def displayEmployee (self):
print “Name:”, self.name, “, Salary:”, self.salary

Create Instance Objects

To create class instances, you call the class using the class name and pass any arguments the init method accepts.

#This would create the first object of Employee class
emp1 = Employee (“Zara”, 2000)
#This would create a second object of Employee class
emp2 = Employee (“Manni”, 5000)

Accessing Attributes

You access object attributes using the dot operator with the object. The class variables will be accessed using the class name as follows:

emp1.displayEmployee()
emp2.displayEmployee()
print (“Total Employee% d”% Employee.empCount)

--

--