Basic Terminologies of OOP in Python.

Basic Terminologies of OOP in Python.

Table of Contents

  • Class
  • Object
  • Methods
  • Class Variables

Class

A class is a blueprint for creating objects, providing initial values for state and implementation of a behavior.

Example:

class Dog:
    pass

Here, we have created an empty dog class. Each dog can have properties like breed, size, age, color, and behaviors like eating, sleeping, sitting, running.

Object:

An individual instance of a particular class.

Example:

class Dog:
    pass

dolly = Dog()

In the above example, dolly is the object of a class Dog. Creating a new object from a class is known as instantiating an object.

Methods:

Methods are functions that are defined inside a class and can only be called from an instance of that class.

Example :

class Dog:
    def __init__(self, name, age):
        self.name = name;
        self.age = age

    def getName(self):
        return self.name

In the above example, there are two methods defined. The __init__() method is special method. Every time the Dog instance is created the __init__() method sets the initial state of that object. The getName() method is a method that returns the name of that dog instance.

Class Variables

Class variables are shared by all instances of the class. Class variables are defined inside of class but outside of methods.

Example:

class Dog:
    breed = 'German Shepherd'

    def __init__(self, name, age):
        self.name = name;
        self.age = age

In the above example, the breed is the class variable. Class variables can be accessed by any method inside the class.