Class and object in python
Class and object in python
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.
Syntax
1. class ClassName:
2. #statement_suite
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:
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:
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.
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.