0
0
Pythonprogramming~5 mins

__init__ method behavior in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the __init__ method in a Python class?
The <code>__init__</code> method initializes a new object when a class is instantiated. It sets up initial values for the object's attributes.
Click to reveal answer
beginner
When is the __init__ method called?
It is called automatically right after a new object is created from a class, without needing to call it explicitly.
Click to reveal answer
beginner
Can the __init__ method take parameters? If yes, why?
Yes, it can take parameters to allow setting different initial values for each new object created from the class.
Click to reveal answer
intermediate
What happens if a class does not have an <code>__init__</code> method?
Python uses a default __init__ method that does nothing, so the object is created but no attributes are set automatically.
Click to reveal answer
beginner
How do you access the object being created inside the __init__ method?
You use the self parameter, which refers to the new object itself, allowing you to set or access its attributes.
Click to reveal answer
What does the __init__ method do in a Python class?
ARuns only when the program ends
BInitializes a new object when the class is instantiated
CDeletes an object
DCreates a new class
Which keyword is used inside __init__ to refer to the current object?
Aself
Bthis
Cobj
Dcurrent
If you do not define an __init__ method, what happens when you create an object?
AThe program crashes
BPython raises an error
CThe object is created with default initialization
DThe object is not created
Can __init__ accept arguments besides self?
ANo, only <code>self</code> is allowed
BOnly if the method is static
COnly if the class inherits another
DYes, to set initial attribute values
When exactly is the __init__ method called?
ARight after an object is created from the class
BWhen the program starts
CWhen the object is deleted
DBefore the class is defined
Explain in your own words what the __init__ method does in a Python class.
Think about what happens when you create a new object.
You got /4 concepts.
    Describe how you would use parameters in the __init__ method and why they are useful.
    Consider how you can give each object its own data.
    You got /3 concepts.