Python Classes and Object-Oriented

  • 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.

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...

 

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