OOPs In Python

amitmund June 04, 2026

Objects

  • You create objects based on the classes.
  • When you write a class, you defined the general behaviour that a whole category of objects can have.

Instantiation

  • Making an object from a call is called instantiation, and you work with instances of a class.

NOTE

  • You can also extend the functionality of existing classes.
  • You can also store your classes in module and import classes written by other programmers in to your own program files.
  • You can model almost anything using classes.

class example

Creating the DOG class

dog.py

class Dog:
    """A simple example to model a dog."""

    def __init__(self,name,age):
        """ Initialize name and age attributes."""
        self.name = name
        self.age = age

    def sit(self):
        """ Simulating a dog sitting."""
        print(f"{self.name} is now sitting.")


    def roll_over(self):
        """Simulating a dog is rolling over."""
        print(f"{self.name} rolled over!")

NOTE

  • Class Name must start with a capital letter. (It should be capitalized).
  • By convention, capitalized names refer to classes in python.

  • The __init__() method, is aspecial method that python run automatically, whenever we create a new instance.

  • We define the __init__() method to have three parameters: self, name and age. As per this example.

  • The self parameter is required in the method definition, and it must come first, before the other parameters.

  • It must be included in the definition because, when python calls this method later (to create an instance of Dog, in this example), the method call will automatically pass the self argument.

  • Every method call associated with an instance automatically passes self, which is reference to the instance itself.

self <--> instance itself

  • it gives the individual instance access to the attributes and methods in the class.

  • What is attributes ?

  • When we make an instance of "Dog", python will call the __init__() method from the Dog class.


0 Likes
10 Views

Filters

No filters available for this view.

Reset All