Skip to content

Latest commit

 

History

History
106 lines (65 loc) · 2.8 KB

Python-Constructor.md

File metadata and controls

106 lines (65 loc) · 2.8 KB

Constructors in Python

Overview of Classes and Objects

  • Class: Similar to Java, a class in Python is a blueprint for objects that defines properties and methods.

  • Object: An instance of a class.

Introduction to Constructors

  • 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.

Example Class: Student

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

Creating an Object

To create an instance of the Student class in Python:

st = Student("Alice", 21, 9.5)

Understanding Constructors

  • 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.

Default Constructor

  • The default constructor in Python initializes each attribute to the values provided in the class definition or to None if not specified.

Manual Constructor

  • 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

Usage:

st = Student("Utkarsh", "JIIT")

Copy Constructor

  • 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

Key Differences from Java

  • 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.

Conclusion

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.