Basic understanding of class in python

Basic understanding of class in python

Basic terminology of object oriented programming

Class

  • A class is a basic design or a plan to an object. Class explains about the properties and behaviour of an object.
  • In other words a class can be defined as it is a blueprint that defines the variables and methods common to all objects of a kind.
  • An object in realworld can be anything that has some properties and behaviour. We generally classify the objects in realworld based on the similarities between them. The same can be appied in the programming world also. Because, every programming language that we use today is developed based on the realworld.
  • For example, we can add all the birds(hen, paret, crow, etc) into a class named "Bird". As we discussed above it's just a blueprint and it's not an object. In realworld "Paret" is an object which comes under the class "Bird".

Object

  • An object is defined as its an realworld entity that has state and behavior. Technically we can say, an object is a bundle of variables and related methods.
  • An object is an outcome of a class or An object is derived from a class.
  • For example, Paret is an object and its an outcome of a class Bird.

Class Vs Object

  • An object is a realworld entity where as class is not
  • An object cannot be inherited but we can inherit the class.
  • For example, Bird is a base class for all the birds. we can classify the birds in to subclasses like Flying Birds and Walking Birds. we can add Paret, Crow under the class Flying Birds and Hen, Penguin under the class Walking Birds.
  • We can inherit the class but we can't inherit an object.

Example: Python class Bank Account

class BankAccount(object):
    def __init__(self, name, account_number, initial_amount):
        self.name = name
        self.account_number = account_number
        self.balance = initial_amount
    def deposit(self, amount):
        self.balance += amount
    def withdraw(self, amount):
        self.balance -= amount
    def get_balance(self):
        return self.balance

ramesh_account = BankAccount('Ramesh', 'TS12345', 500)
suresh_account = BankAccount('Suresh', 'TS12345', 1500) # another object of same class
print(ramesh_account.account_number)
# TS12345
print(ramesh_account.get_balance())
# 500
ramesh_account.withdraw(100)
print(ramesh_account.get_balance())
# 400
ramesh_account.deposit(150)
print(ramesh_account.get_balance())
# 550

In above code we have created a class "BankAccount". If we consider the general scenario every bank account has properties like its bank holders name, bank account number, bank account initial amount and it has operations withdraw an amount, deposit an amount and check the available balance. If we convert this scenario into programming then all the properties will become attributes and operations will become methods. In python to create a class we have to use the keyword "class" then followed by a space and then class name after colon(:). Follow the link https://docs.python.org/3/tutorial/classes.html#class-definition-syntax to learn about python class syntax. We can have multiple instances for a single class. To create an instance of a class we can simply call the class with initial parameters. When we call the class with initial parameters then it will initialize the object with initial parameters and binds the respect methods associated to the class. In above code we have used code "ramesh_account = BankAccount('Ramesh', 'TS12345', 500)" to create the class instance. To call a method we can simply use the instance. In above code we have used the method "get_balance" to get the balance of an account. To access the attribute of class we can simply use dot(.) notaion on object. In above code, we accessed attribute "account_number" like "ramesh_account.account_number".