Lesson 46 of 60
โฎPrev Nextโฏ

PY-101 ยท Object-oriented Python

__init__ and attributes

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

46of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    __init__ is a special method that runs automatically when an object is created, and it sets up the object's attributes. Its first parameter, self, is the object being built, so self.name = name stores the name on that object. After that, obj.name reads it back.

    • Write __init__
    • Understand self
    • Set and read attributes

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    __init__ and attributes, running
    1. class Student:
    2. def __init__(self, name, roll, marks=0):
    3. self.name = name
    4. self.roll = roll
    5. self.marks = marks
    6. asha = Student("Asha", 12, 91)
    7. ravi = Student("Ravi", 7)
    8. print(asha.name, asha.roll, asha.marks)
    9. print(ravi.name, ravi.marks)
    10. ravi.marks = 78
    11. print("Updated:", ravi.marks)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemCreating a student and then remembering to add name, roll number and marks one by one is error-prone. __init__ makes the class demand them up front.
    2. The Python ideaclass Name:__init__ is a special method that runs automatically when an object is created, and it sets up the object's attributes.
    3. In real lifeAPI client libraries take your key and settings in __init__: client = OpenAI(api_key=...), then every call uses self.api_key.

    Start with a question

    Creating a student and then remembering to add name, roll number and marks one by one is error-prone. __init__ makes the class demand them up front.

    Almost every class you write or read starts with __init__. It defines what an object holds.

    Explain it like I'm 10

    self is the object saying "me". self.name = name means "write this name on MY label".

    The technical explanation

    def __init__(self, name, roll): runs when you call Student("Asha", 12).

    Python passes the new object as self automatically; you pass only the other arguments.

    Attributes can have defaults: def __init__(self, name, marks=None):.

    Syntax

    class Name:
        def __init__(self, a, b=0):
            self.a = a
            self.b = b

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    API client libraries take your key and settings in __init__: client = OpenAI(api_key=...), then every call uses self.api_key.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. When does __init__ run?
    2. 2. What is self?
    3. 3. Student("Asha", 12) passes how many arguments yourself?
    4. 4. Inside __init__, name = name (no self.) does what?
    5. 5. How do you read an attribute?

    Questions people ask

    Is __init__ a constructor in Python?

    It is usually called the constructor. Strictly, __new__ creates the object and __init__ initialises it, but you will almost always only write __init__.

    Why is self needed in Python methods?

    Python passes the object explicitly so methods know which object they are working on. The name self is a convention, but always use it.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner Is __init__ a constructor in Python?

    It is usually called the constructor. Strictly, __new__ creates the object and __init__ initialises it, but you will almost always only write __init__.

    beginner Why is self needed in Python methods?

    Python passes the object explicitly so methods know which object they are working on. The name self is a convention, but always use it.

    intermediate When does __init__ run?

    When an object is created. Calling the class runs __init__.

    intermediate What is self?

    The object being created or used. self is the current object.

    Quick revision

    Remember

    • Write __init__
    • Understand self
    • Set and read attributes
    • Forgetting self. is the classic bug: name = name inside __init__ only creates a local variable that disappears.

    Syntax

    class Name:
        def __init__(self, a, b=0):
            self.a = a
            self.b = b

    Official documentation