0
0
Pythonprogramming~5 mins

Instance attributes in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are instance attributes in Python?
Instance attributes are variables that belong to a specific object created from a class. Each object can have different values for these attributes.
Click to reveal answer
beginner
How do you define an instance attribute inside a class?
You define instance attributes inside the __init__ method using self, like: <br>self.attribute_name = value.
Click to reveal answer
intermediate
What is the difference between instance attributes and class attributes?
Instance attributes belong to each object separately, while class attributes are shared by all objects of the class.
Click to reveal answer
intermediate
Can instance attributes be added outside the __init__ method?
Yes, you can add or change instance attributes anytime using dot notation, like object.attribute = value.
Click to reveal answer
beginner
What happens if you try to access an instance attribute that does not exist?
Python raises an AttributeError because the object does not have that attribute.
Click to reveal answer
Where are instance attributes usually defined in a Python class?
AInside a static method
BOutside the class
CAs global variables
DInside the __init__ method using self
What keyword is used to refer to the current object inside a class?
Aself
Bthis
Cme
Dobj
If you change an instance attribute on one object, does it affect other objects of the same class?
AOnly if the attribute is a class attribute
BYes, all objects change
CNo, only that object changes
DOnly if the attribute is private
What error occurs if you try to access a missing instance attribute?
AAttributeError
BNameError
CTypeError
DValueError
Can you add new instance attributes after an object is created?
ANo, attributes must be defined in __init__
BYes, using dot notation on the object
COnly if the class allows it explicitly
DOnly for class attributes
Explain what instance attributes are and how they differ from class attributes.
Think about how each object can have its own data.
You got /3 concepts.
    Describe how to create and access instance attributes in a Python class.
    Remember the role of self inside methods.
    You got /3 concepts.