-
Class: Similar to Java, a class in Python is a blueprint for objects that defines properties and methods.
-
Object: An instance of a class.
- A constructor in Python is a special method called
__init__
used for initializing new objects. It's analogous to constructors in Java and is called when a class instance is created.
Here's how you would define a simple Student class in Python:
class Student:
def __init__(self, name=None, age=0, psp=0.0):
self.name = name
self.age = age
self.psp = psp
To create an instance of the Student class in Python:
st = Student("Alice", 21, 9.5)
-
The
__init__
method in Python serves as the constructor. -
If
__init__
is not explicitly defined, Python provides a default one that doesn't do anything.
- The default constructor in Python initializes each attribute to the values provided in the class definition or to None if not specified.
- Python allows creating manual constructors for more control during object initialization.
class Student:
def __init__(self, student_name, university_name):
self.name = student_name
self.univ_name = university_name
st = Student("Utkarsh", "JIIT")
- Python doesn't have a built-in copy constructor like Java. However, you can define a method to copy an object manually.
class Student:
def __init__(self, other=None):
if other:
self.name = other.name
self.age = other.age
else:
self.name = None
self.age = 0
# Method to manually create a copy of the object
def copy(self):
return Student(self)
Usage:
st1 = Student()
st1.name = "Utkarsh"
st1.age = 27
st2 = st1.copy() # Creating a copy using the copy method
-
Python's
__init__
method is more flexible compared to Java's constructors. -
In Python, the concept of a copy constructor is not built-in and needs to be implemented manually.
-
Python does not have different types of constructors (default, parameterized, copy constructor) in the same way Java does. Instead, Python utilizes default arguments to achieve similar functionality.
Python's approach to constructors and object initialization is more streamlined compared to Java, using the __init__
method with default parameters to provide a great deal of flexibility and simplicity. While it lacks a built-in copy constructor, Python's dynamic nature allows for easy implementation of copying functionality.