Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Do you think declarations within Python class are equivalent to those within __init__ method?
In Python, the declarations within a class are not equivalent to those within the __init__() method. Class declarations create class attributes shared by all instances, while __init__() declarations create instance attributes specific to each object.
A class is a collection of objects that contains the blueprints or prototype from which objects are created. It is a logical entity that contains attributes and methods.
Python __init__() Method
The Python __init__() function is a constructor method called automatically whenever an object is created from a class. Constructors are used to initialize objects by assigning values to the data members when an object is instantiated.
Class Declaration Syntax
A class is created using the keyword class followed by a class name in Pascal case ?
class ClassName:
# statements
pass
Example
Here's a simple class declaration with a print statement ?
class Animal:
print("I am Animal")
obj1 = Animal()
I am Animal
__init__() Method Declaration
The __init__() method follows this syntax ?
class ClassName:
def __init__(self):
# initialization statements
pass
Example
When an object is created, the __init__() method is automatically called ?
class Tutorialspoint:
def __init__(self):
print("Hello welcome to Python course")
obj1 = Tutorialspoint()
Hello welcome to Python course
Class Attributes vs Instance Attributes
There are two types of attributes based on where they are declared:
- Class Attributes: Defined at the class level and shared between all instances. Accessed using the class name.
- Instance Attributes: Defined inside __init__() and specific to each object. Accessed using the object name.
Example
This example demonstrates the difference between class and instance attributes ?
class Student:
# Class Attribute
school_name = "Oxword"
def __init__(self, name, grade):
# Instance Attributes
self.name = name
self.grade = grade
# Creating two student objects
s1 = Student("John", "8th")
s2 = Student("Annie", "9th")
# Accessing attributes
print(s1.name, s1.grade, s1.school_name)
print(s2.name, s2.grade, s2.school_name)
# Changing class attribute using class name
Student.school_name = "Maple Leaf School"
# Changing instance attribute
s1.name = "Roshan"
# Displaying updated values
print(s1.name, s1.grade, s1.school_name)
print(s2.name, s2.grade, s2.school_name)
John 8th Oxword Annie 9th Oxword Roshan 8th Maple Leaf School Annie 9th Maple Leaf School
Key Differences
| Aspect | Class Declarations | __init__() Declarations |
|---|---|---|
| Scope | Shared by all instances | Specific to each instance |
| Access | ClassName.attribute | object.attribute |
| When created | Class definition time | Object instantiation time |
| Memory | Single copy for all | Separate copy per object |
Conclusion
Class declarations create shared attributes for all instances, while __init__() declarations create unique attributes for each object. Understanding this difference is crucial for proper object-oriented programming in Python.
