__init__

Dunder MethodPython 2.0+Beginner

Constructor; called when a new instance is created to initialize its attributes

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed

    def __str__(self):
        return f"{self.name} ({self.breed})"

dog = Dog("Rex", "German Shepherd")
print(dog)
print(dog.name)

__init__ constructor; called when a new instance is created to initialize its attributes. Implementing it lets you customize how Python interacts with your objects.

Try in Playground

Tags

oopmagic-methodprotocolcore