Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Python

Constructors in Python

Initialize object state with the __init__ constructor method.

Constructors in Python — a practical guide to Python __init__ constructor with clear examples you can reuse in real projects.

Python Tutorial Series (46/95). Prefer one article? Read the complete Python tutorial.

Short description

`__init__` runs when you create an object. Set required attributes there.

__init__ example

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

acc = BankAccount('Asha', 1000)
print(acc.owner, acc.balance)

Leave a reply

Your email address will not be published. Required fields are marked *