Table of Contents
Introduction to Class And Object in Python
Python is an object-oriented programming language its is a way of computer programming using the idea of “Class and objects” to represent data and methods. It is also, an approach used for creating neat and reusable code rather than a redundant one. The Python program is split into self-contained objects or several mini-programs. Every Individual a special a part of the appliance having its own logic and data to communicate within themselves.
An object is a collection of variable and function method that acts on the data and class is a blueprint for that object.
We can think of a class as a sketch (Prototype) of the product. It contains height, Weight, Dimension, etc. best on this description od data we build the sketch of the product. product is the object. As many products can be made from a product blueprint. We can create many objects from a class. An object is called the instance of a python class and the process of creating this object is called instantiation.
Define a class in Python
To Create class use keyword ‘class’
Class have an attribute as variables.
Class attribute always public can be accessed by using Dot( . ).
Example:- class.attribute
Syntex:-
Class class_name:
Statement -1
.
.
.
Statement-n
Example:-
class snake:
pass
In the above example, the class keyword indicates that you are creating a class followed by the name of the class (snake in this case).
Example 2:- Create a class pass attribute into it and add function into it to return a string.
class Details:
"This is a Detail class"
age = 10
def name(self):
print('Jhon')
# Output: 10
print(Details.age)
# Output: <function Details.name>
print(Details.name)
# Output: This is a Detail class’
print(Details.__doc__)
Output:-
10
<function Details.name at 0x0000021BD5D42048>
This is a Detail class
Class Object
An object is a collection of variable and function method that acts on the data and class is a blueprint for that object. The class object could be used to access different attributes.it is also used to create new object instances of the class. Creating an object is similar to a function call.
Example:- Create a class of details and call it by creating object of class
class Details:
"This is a Detail class"
age = 10
def name(self):
print('Jhon')
# create a new object of Details class
Obj = Details()
# Output: <function Details.name>
print(Details.name)
# Output: <bound method Details.name of <__main__.Details object>>
print(Obj.name)
# Calling object's name() method
# Output: Jhon
Obj.name()
# Call Variable age using Object
#Output:- 10
print(Obj.age)
#__doc__ gives us the docstring of that class
#Output:-This is a Detail class
print(Obj.__doc__)
Output:-
<function Details.name at 0x0000021BD5DE8048>
<bound method Details.name of <__main__.Details object at 0x0000021BD5DE5320>>
Jhon
10
This is a Detail class
Methods in class
The Self
In the class method have extra first parameter in method definition. we don’t give the value for this parameter when we call the method, python provide it directly.
If we have a method that taken not an argument, then we still have to have one argument.
When we call a method of this object as an object.method(argument1,argument2), this is automatically converted by Python into Class.method(object, argument1,argument2)
__init__ method
The python __init__ method is similar to constructors in C++ and Java. Constructor is used to initialize the object state. Like methods, a constructor also contains a group of statements(i.e. instructions) that are executed at the time of when Object creation. It is run as soon as an object of a class is instantiated. The method is helpful to try and do any initialization you wish to do along with your object.
Example:-
# A Sample class with init method
class Details:
# init method or constructor
def __init__(self, name, age):
self.name = name
self.age = age
# Sample Method
def say_hello(self):
print('Hello, My Name is', self.name,'\nMy Age is', self.age)
p = Details('Rahul',28)
p.say_hello()
Output:-
Hello, My Name is Rahul
My Age is 28
Class and Instance Variables
Instance variables are from unique to each data instance and class variable re for attributes in class and the methods share all instances of the given class. Instance variables are the variables whose value is assigned inside into a constructor or method with self whereas class variables are variables whose value is assigned in the class.
Example:-
Defining instance variable using constructor.
#Program to show that the variables with a value
# assigned in the class declaration, are class variables and
# variables inside methods and constructors are instance
# variables.
# Class for Dog
class Animal:
# Class Variable
animal = 'Dog'
# The init method or constructor
def __init__(self, breed, color):
# Instance Variable
self.breed = breed
self.color = color
# Objects of Animal class
Bhruno = Animal("german shepherd", "brown")
Rambow = Animal("black del", "black")
print('Bhruno details:')
print('Bhruno is a', Bhruno.animal)
print('Breed: ', Bhruno.breed)
print('Color: ', Bhruno.color)
print('\nRambow details:')
print('Rambow is a', Rambow.animal)
print('Breed: ', Rambow.breed)
print('Color: ', Rambow.color)
# Class variables can be accessed using class
# name also
print("\nAccessing class variable using class name")
print(Animal.animal)
Output:-
Bruno details:
Bruno is a Dog
Breed: german shepherd
Color: brown
Rambow details:
Rambow is a Dog
Breed: black del
Color: black
Accessing class variable using class name
Dog
Example 2:– Defining instance variable using the normal method in python.
# Program to show that we can create instance variables inside methods
# Class for Animal
class Animal:
# Class Variable
animal = 'Dog'
# The init method or constructor
def __init__(self, breed):
# Instance Variable
self.breed = breed
# Adds an instance variable
def setColor(self, color):
self.color = color
# Retrieves instance variable
def getColor(self):
return self.color
# Driver Code
Rodger = Animal("Bhruno")
Rodger.setColor("Brown")
print(Rodger.getColor())
Output:-
Brown
Conclusion:-
In this blog, you will get knowledge about how to create classes and objects in python with different methods. And the example gives you a better understanding of the syntax to create class and object in python.