Overview of OOP Terminology:-

Overview of OOP Terminology:-

Let’s understand the common terminologies used in OOP.

What is a Class?

In simple words, class can be thought of as a logical grouping of data and functions .

classes are used to keep related things together. This is done using the keyword “class,” which is a grouping of object-oriented constructs. While def is used to define a function, class is used to define a class.

In example below we’ve created an empty class called “DemoClass”.

class DemoClass(object): 
      pass 

What is a Method?

Functions defined within a class are called “Methods”. When you define methods, you will need to always provide the first argument to the method with a self keyword.

In the example below displayCount is a method of class “Employee”.

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 

Attributes:

The attributes are data members (class variables and instance variables) and methods, accessed via dot notation. Attributes holds data associated with a class and its objects.

As an object-oriented language, Python provides two scopes for attributes: class attributes and instance attributes.

  • An instance attribute is a Python variable belonging to one, and only one, object. This variable is only accessible in the scope of this object and it is defined inside the constructor function, __init__(self,..) of the class.
  • A class attribute is a Python variable that belongs to a class rather than a particular object. It is shared between all the objects of this class and it is defined outside the constructor function, __init__(self,…), of the class.

In the example below class_attr & inst_att  are the Data members of class “DemoClass”.

The below DemoClass is a basic Python class with two attributes: class_attr and inst_attr. 

class DemoClass(object):
       class_attr = 0 
     def __init__(self, inst_attr): 
           self.inst_attr = inst_attr 

  • inst_attr is an instance attribute defined inside the constructor.
  • class_attr is a class attribute defined outside the constructor.

The inst_attr is only accessible from the scope of an object. The class attribute (class_attr) is accessible as both a property of the class and as a property of objects, as it is shared between all of them.

Object:-

A unique instance of a data structure that’s defined by its class. An object comprises both data members (class variables and instance variables) and methods. Once one class is created  we can use this class to create objects  An object is an instance of a class.

Instance :−

An individual object of a certain class. An object obj that belongs to a class Employee, for example, is an instance of the class Employee.

To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts.

Following is the example of a simple Python class −

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 
  

"This would create first object of Employee
class" 

emp1 = Employee("Zara", 2000)

"This would create second object of Employee class"

emp2 = Employee("Manni", 5000)

So to summarise , In the example given above –

  • Employee – is a class.
  • displayCount & displayEmployee – are the methods.
  • empCount , name & salary – are the Attributes (empCount- Class Variable & name , salary- Instance variable).
  • emp1 & emp2 – is an object.
  • emp1 object is an instance of class Employee.

Built-In Class Attributes

Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute −

  • __dict__ − Dictionary containing the class’s namespace.
  • __doc__ − Class documentation string or none, if undefined.
  • __name__ − Class name.
  • __module__ − Module name in which the class is defined. This attribute is “__main__” in interactive mode.
  • __bases__ − A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.

For the above class let us try to access all these built-in class attributes −

print "Employee.__doc__:", Employee.__doc__
print "Employee.__name__:", Employee.__name__
print "Employee.__module__:", Employee.__module__
print "Employee.__bases__:", Employee.__bases__
print "Employee.__dict__:", Employee.__dict__

When the above code is executed, it produces the following result −

Employee.__doc__: Common base class for all
employees

Employee.__name__: Employee

Employee.__module__: __main__

Employee.__bases__: ()

Employee.__dict__: {'__module__': '__main__', 'displayCount':

<function displayCount at 0xb7c84994>, 'empCount': 2,

'displayEmployee': <function displayEmployee at 0xb7c8441c>,

'__doc__': 'Common base class for all employees', 

'__init__': <function __init__ at 0xb7c846bc>}

Leave a Reply