FAQ's

Let’s answer some FAQ’s


❓ Can I create a class without a constructor?

Yes, totally.

If you don’t write __init__, Python creates a blank one for you (default constructor)

But… then your object won’t have any properties unless you set them later.


❓ Can I pass default values in the constructor?

Yes yes!

class BankAccount:
    def __init__(self, holder="Unknown", balance=0):
        self.holder = holder
        self.balance = balance

Now you can do both:

a = BankAccount("Ali", 1000)
b = BankAccount()  # holder=Unknown, balance=0

❓ Can I write logic inside the constructor?

Yes.

A constructor is like a setup area.
You can write any logic that you want to run when the object is being created.

def __init__(self, holder, balance):
    self.holder = holder
    if balance < 0:
        self.balance = 0
    else:
        self.balance = balance

So if someone tries to create account with negative money → system fixes it silently


❓ Can I have multiple constructors?

In Python, no.
Python doesn't support method overloading like Java.

So you can't do this:

def __init__(self, holder):  # ❌
def __init__(self, holder, balance):  # ❌

But…

You can handle it smartly using default values or *args

def __init__(self, holder, balance=0):
    self.holder = holder
    self.balance = balance

❓ Can a constructor call other methods?

Of course!

def __init__(self, holder, balance):
    self.holder = holder
    self.balance = balance
    self.show_details()

def show_details(self):
    print(f"{self.holder} has balance {self.balance}")

So when you create an object → constructor triggers → method runs inside the constructor 💥

Updated on