Python File handling

Python Class & Object

  • The entire OOPs methodology has been derived from an Object which is root concept of OOP.
  • An Object is anything which have real world existence and can be differentiate from each other by means of behavior or appearance.
  • For Example, a table, a car, a pen, a person etc.
  • It is possible that some objects may have similar behavior and such objects belong to same category which is known as Class.
  • For Example, Audi, BMW, Mercedes are belongs to Car Class .
Python Class & Object
Python Class & Object

Class

  • A Class is a model, blue print or plan to create an Object. That means we write attributes and action in python Class for Object.
  • A Class contains variables and methods which are also available in object because Objects are instance of a Class that means Objects are created from Class.
  • In Python Class is created with class keyword and class name.
class Car:
    #Block of Attributes
    def __init__(self):
       self.model = "A8 L"
       self.color = "Red"

    #Block of Methods
    def start(self):
       print(self.model," Car is Started!")
       print("It's ",self.color," Color Car")

    def stop(self):
       print(self.model," Car is Stopped!")
  • We can also write object after Class name, that represents the base class name from where all Python classes are derived.
  • Writing Object with class name is not compulsory since it is implied.
class Car(object):
  #writing object with classname is not compulsory
    #Block of Attributes
    def __init__(self):
       self.model = "A8 L"
       self.color = "Red"

    #Block of Methods
    def start(self):
       print(self.model," Car is Started!")
       print("It's ",self.color," Color Car")

    def stop(self):
       print(self.model," Car is Stopped!")
  • In above example we declare class with class keyword and here our class name is Car. Classname should be in capital letter.
  • In Python we cannot declare variable as we do in C++ and Java like OOP languages.
  • In Python we can write our variable inside a special method __init__(). This method is allow us to initialize our class variables in our case we have two variables model and color.
  • We have passed self keyword with __init__() as parameter, this self keyword refers to instance of current class.
  • Our class have 2 methods which are start() and stop() with one self parameter here also self represents calling object of this method.

Object

  • An object is an instance of Class. As we learned before class is just blue print of object and class does not have any existence in memory until object is created.
  • When we create Object for a class, a separate memory block is allocated on the heap and memory and that location is stored as self.
  • The instance contains the variables i.e. model, color which is known as instance variables and methods start() and stop() are known as instance methods.
  • Instance methods use ‘self’ keyword as first parameter that refers memory location of an instance.
  • Let’s create object for above class.
audi = Car()
  • Here, audi is the instance of Car class. We can create as much as Objects we required for Car class.
  • when we create new object following steps take place internally.
  1. First of all memory block is allocated on heap. Memory is allocated based on
  2. After memory allocation, special __init()__ method is called internally. and initialize all instance variables. This method is also known as constructor because it is useful to build instance.
  3. At last allocated memory location is returned to class instance. We can check memory address by id(audi).
print(audi.model)  # A8 L
print(audi.color)  # red
audi.start()       # A8 L Car is Started!
                   # It's red Color Car
audi.stop()        # A8 L Car is Stopped!
  • We can access instance variables and method using object and the dot operator.
  • The dot operator takes instance name at the left hand and members of an instance at the right hand side

Leave a Comment

Your email address will not be published. Required fields are marked *