← Back to Python Course | Chapter 7: OOP | Lesson 3 of 11

Python Constructors

A constructor is the setup step that runs when a new object is born, like filling in a name tag when a new student arrives. It gives the object its starting values.
Syntax
python
class ClassName:
    def __init__(self, parameter):
        self.attribute = parameter

object_name = ClassName(argument)

What is a Constructor?

A constructor is the method Python calls automatically the moment an object is created, and in Python that's __init__ -- despite the name, it doesn't allocate the object (that's __new__ behind the scenes), it just initializes the attributes on the object that already exists.

Example: What is a Constructor?

python
class Dog:
    def __init__(self):  # __init__ runs automatically when a Dog is created
        print("A new dog is created")

Dog()

Default Constructor

A constructor that only takes self and no other parameters gives every new object the exact same starting values, which is fine for simple cases but means you can't customize an object at creation time -- every Dog() would start with an identical name, for instance.

Example: Default Constructor

python
class Dog:
    def __init__(self):
        self.name = "Unnamed"  # every Dog starts with the same fixed name

d = Dog()
print(d.name)

Parameterized Constructor

Accepting extra parameters beyond self (def __init__(self, name, breed):) lets each call to the class supply its own values, so Dog(Rex, Labrador) and Dog(Milo, Poodle) end up as genuinely different objects from the same blueprint.

Example: Parameterized Constructor

python
class Dog:
    def __init__(self, name, breed):  # extra parameters let each call customize the object
        self.name = name
        self.breed = breed

rex = Dog("Rex", "Labrador")
print(rex.name, rex.breed)

Default Parameter Values

Giving a constructor parameter a default value (def __init__(self, name=Unnamed):) makes that argument optional -- callers who don't pass a name get the fallback, while callers who do pass one override it, without needing two separate constructors.

Example: Default Parameter Values

python
class Dog:
    def __init__(self, name="Unnamed"):  # default makes the name argument optional
        self.name = name

print(Dog().name)  # uses the default
print(Dog("Rex").name)  # overrides the default

The Role of self

self refers to the specific object being built or acted on right now, which is why 'self.name = name' inside __init__ stores the passed-in value onto that particular instance rather than some shared or global variable -- every method needs self to know which object it's operating on.

Example: The Role of self

python
class Dog:
    def __init__(self, name):
        self.name = name  # self refers to this specific Dog being created

d = Dog("Rex")
print(d.name)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Misspelling the constructor as __innit__ or _init_, so it never runs and the object has no attributes.
  2. Forgetting to write self. when storing values, as in name = name, which only sets a local variable and leaves the object empty.
  3. Returning a value from __init__, such as return self, which raises TypeError because it must return None.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.