Object oriented programming in python

  • Python is an object-oriented programming language, which means that it manipulates and works with data structures called objects.
  • Objects can be anything that could be named in Python – integers, functions, floats, strings, classes, methods, etc.
  • These objects have equal status in Python. They can be used anywhere an object is required.
  • You can assign them to variables, lists, or dictionaries. They can also be passed as arguments. Every Python object is a class.
  • A class is simply a way of organizing, managing, and creating objects with the same attributes and methods.
  • Like many aspects of programming it is necessary to learn the concepts of object oriented programming before you can use them effectively.
  • So approach this chapter as a way to learn some terms and concepts and work through a few simple examples to lay a foundation for future learning.
  • Throughout the rest of the book we will be using objects in many of the programs but we won’t be building our own new objects in the programs.
  • Python is an object-oriented programming language, which means that it manipulates and works with data structures called objects.
  • Objects can be anything that could be named in Python – integers, functions, floats, strings, classes, methods, etc.
  • These objects have equal status in Python. They can be used anywhere an object is required.
  • You can assign them to variables, lists, or dictionaries. They can also be passed as arguments. Every Python object is a class.
     Introduction-to-Object-Oriented-Programming

Object

  • An Object is a bit of self-contained Code and Data.
  • A key aspect of the Object approach is to break the problem into smaller understandable parts (divide and conquer).
  • Objects have boundaries that allow us to ignore un-needed detail.
  • We have been using objects all along: String Objects, Integer Objects, Dictionary Objects, List Objects...
  • At its simplest, an object is some code plus data structures that is smaller than a whole program
  • Defining a function allows us to store a bit of code and give it a name and then later invoke that code using the name of the function.
  • An object can contain a number of functions (which we call “methods”) as well as data that is used by those functions. We call data items that are part of the object “attributes”
  • We use the class keyword to define the data and code that will make up each of the objects
  • The class keyword includes the name of the class and begins an indented block of code where we include the attributes (data) and methods (code).

 

Class Syntax

To define a class, you can use ‘class’, a reserved keyword, followed by the classname and a colon. By convention, all classes start in uppercase. For example:

 class Students:
    pass 

//To create a class that takes an object: 

 class Students(object)
   

The __init__() method

Immediately after creating an instance of the class, you have to call the __init__() function. This function initializes the objects it creates. It takes at least the argument ‘self’, a Python convention, which gives identity to the object being created.

Example

class Students:
  def __init__(self) :

class Employees(object):
  def __init__(self, name, rate, hours) : 
   
//A function used in a class is called a method. Hence, the __init__() function is a method when it is used to initialize classes.

 

Instance Variables

When you add more arguments to the def_init_() besides the self, you’ll need to add instance variables so that any instance object of the class will be associated with the instance you create.

Example

class Employees(object):
  def __init__(self, name, rate, hours) :
  name.self = name
  rate.self = rate
  hours.self =hours 
  
staff = Employees(“Wayne”, 20, 8)
supervisor = Employees(“Dwight”, 35, 8)
manager = Employees(“Melinda”, 100, 8)

print(staff.name, staff.rate, staff.hours) 
print(supervisor.name, supervisor.rate, supervisor.hours) 
print(manager.name, manager.rate, manager.hours)

Output

 
 Wayne 20 8 
 Dwight 35 8
 Melinda 100 8

 

Inheritance

Inheritance is a Python process that allows one class to take on the methods and attributes of another.

This feature allows users to create more complicated classes that inherit methods or variables from their parent or base classes and makes programming more efficient.

Syntax

class ChildClass(ParentClass): 

Example

class Employees(object):
  def __init__(self, name, rate, hours):
  self.name = name 
  self.rate = rate 
  self.hours = hours 
  
staff = Employees(“Wayne”, 20, 8) 
supervisor = Employees(“Dwight”, 35, 8)
manager = Employees(“Melinda”, 100, 8) 

print(staff.name, staff.rate, staff.hours) 
print(supervisor.name, supervisor.rate, supervisor.hours)
print(manager.name, manager.rate, manager.hours)

 class Resigned(Employees): 
    def __init__ (self, name, rate, hours, status):
    self.name = name
    self.rate = rate
    self.hours = hours 
    self.status = status

exemp_1 = Resigned(“Dorothy”, 32, 8, “retired”)
exemp_2 = Resigned(“Malcolm”, 48, 8, “resigned”)

print(exemp_1.name, exemp_1.rate, exemp_1.hours, exemp_1.status)
print(exemp_2.name, exemp_2.rate, exemp_2.hours, exemp_2.status)

Output

 Wayne 20 8
 Dwight 35 8
 Melinda 100 8
 Dorothy 32 8 retired
 Malcolm 48 8 resigned