1/9
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What does this code define?
class Dog:
A class (blueprint for objects).
Look for class
keyword.
Identify the instance attribute:
def __init__(self):
self.name = "Fido"
self.name
(unique to each object).
Key: Any variable prefixed with self.
.
What’s the class attribute?
class Dog:
species = "Canine"
species
(shared by all Dog
objects).
Key: Variables declared inside class but outside methods.
Which method is the constructor?
def __init__(self, age):
self.age = age
__init__
(auto-called during instantiation).
Key: Always named __init__
.
Is this a method or function?
def bark(self):
print("Woof!")
Method (defined inside a class, takes self
).
Key: Functions are standalone; methods belong to classes.
What does __str__
do?
Magic Method, defines how the object is printed (e.g., print(dog)
).
Key: Look for __method__
names.
Find the local variable:
def calculate(self):
total = self.price * 2
total
(exists only inside the method).
Key: Variables not attached to self
.
What’s special here?
@classmethod
def change_species(cls, new_species):
cls.species = new_species
Class method (modifies class-level attributes via cls
).
Key: Decorator @classmethod
and cls
parameter.
How do you create a Dog
object?
my_dog = Dog()
(calls __init__
).
Key: ClassName()
syntax.
Why does every method need self
?
self
refers to the current object instance.
Key: First parameter in instance methods.