0% found this document useful (0 votes)
2 views5 pages

Class and object in python

This document explains the concepts of classes and objects in Python, highlighting their role in object-oriented programming. It details how to create classes, instantiate objects, and differentiate between class and instance variables. The article emphasizes the importance of these concepts for writing reusable and manageable code.

Uploaded by

reddisandeep92
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
2 views5 pages

Class and object in python

This document explains the concepts of classes and objects in Python, highlighting their role in object-oriented programming. It details how to create classes, instantiate objects, and differentiate between class and instance variables. The article emphasizes the importance of these concepts for writing reusable and manageable code.

Uploaded by

reddisandeep92
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Classes and Objects in Python

Python is an object-oriented programming language that offers classes,


which are a potent tool for writing reusable code. To describe objects with
shared characteristics and behaviours, classes are utilised. We shall examine
Python's ideas of classes and objects in this article.

Classes in Python:
In Python, a class is a user-defined data type that contains both the data
itself and the methods that may be used to manipulate it. In a sense, classes
serve as a template to create objects. They provide the characteristics and
operations that the objects will employ.

Suppose a class is a prototype of a building. A building contains all the


details about the floor, rooms, doors, windows, etc. we can make as many
buildings as we want, based on these details. Hence, the building can be
seen as a class, and we can create as many objects of this class.

Creating Classes in Python


In Python, a class can be created by using the keyword class, followed by the
class name. The syntax to create a class is given below.

Syntax

1. class ClassName:
2. #statement_suite

 In Python, we must notice that each class is associated with a documentation


string which can be accessed by using <class-name>.__doc__.
 A class contains a statement suite including fields, constructor, function, etc.
definition.

Example:
Code:

1. class Person:
2. def __init__(self, name, age):
3. # This is the constructor method that is called when creating a new Per
son object
4. # It takes two parameters, name and age, and initializes them as attrib
utes of the object
5. self.name = name
6. self.age = age
7. def greet(self):
8. # This is a method of the Person class that prints a greeting message
9. print("Hello, my name is " + self.name)
Name and age are the two properties of the Person class. Additionally, it has a
function called greet that prints a greeting.

Objects in Python:
 An object is a particular instance of a class with unique characteristics
and functions. After a class has been established, you may make
objects based on it.
 By using the class constructor, you may create an object of a class in
Python.
 The object's attributes are initialised in the constructor, which is a
special procedure with the name __init__.

Syntax:

1. # Declare an object of a class


2. object_name = Class_Name(arguments)

Example:

Code:

1. class Person:
2. def __init__(self, name, age):
3. self.name = name
4. self.age = age
5. def greet(self):
6. print("Hello, my name is " + self.name)
7.
8. # Create a new instance of the Person class and assign it to the variable pers
on1
9. person1 = Person("Ayan", 25)
10. person1.greet()

Output:

"Hello, my name is Ayan"

The self-parameter
The self-parameter refers to the current instance of the class and accesses
the class variables. We can use anything instead of self, but it must be the
first parameter of any function which belongs to the class.

_ _init_ _ method
 In order to make an instance of a class in Python, a specific function
called __init__ is called. Although it is used to set the object's
attributes, it is often referred to as a constructor.
 The self-argument is the only one required by the __init__ method.
 This argument refers to the newly generated instance of the class.
 To initialise the values of each attribute associated with the objects,
you can declare extra arguments in the __init__ method.

Class and Instance Variables


 All instances of a class exchange class variables.
 They function independently of any class methods and may be
accessed through the use of the class name.
 Here's an illustration:

Code:

1. class Person:
2. count = 0 # This is a class variable
3.
4. def __init__(self, name, age):
5. self.name = name # This is an instance variable
6. self.age = age
7. Person.count += 1 # Accessing the class variable using the name of th
e class
8. person1 = Person("Ayan", 25)
9. person2 = Person("Bobby", 30)
10. print(Person.count)

Output:

Whereas, instance variables are specific to each instance of a class. They are
specified using the self-argument in the __init__ method. Here's an
illustration:

Code:

1. class Person:
2. def __init__(self, name, age):
3. self.name = name # This is an instance variable
4. self.age = age
5. person1 = Person("Ayan", 25)
6. person2 = Person("Bobby", 30)
7. print(person1.name)
8. print(person2.age)

Output:

Ayan
30

Class variables are created separately from any class methods and are
shared by all class copies. Every instance of a class has its own instance
variables, which are specified in the __init__ method utilising the self-
argument.

Conclusion:
In conclusion, Python's classes and objects notions are strong ideas that let
you write reusable programmes. You may combine information and
capabilities into a single entity that is able to be used to build many objects
by establishing a class. Using the dot notation, you may access an object's
methods and properties after it has been created. You can develop more
logical, effective, and manageable code by comprehending Python's classes
and objects.

You might also like