Objects and lazy iteration · Lesson 21 of 25
Classes and objects
Group state and behavior in one object.
8 minutes2 games
A class defines objects. __init__ initializes an instance after creation, and self refers to the current instance. Store instance state on self and call methods through the object.
Python
class Counter:
def __init__(self, start=0):
self.value = start
def increment(self):
self.value += 1
return self.value
a = Counter()
b = Counter(10)
print(a.increment())
print(b.value)a.value1int
b.value10int
Complete the code
Complete storing the student name.
class Student: def __init__(self, name): .name =
Exercise
Define Box with a width and a double method returning twice the width. Print Box(4).double().
Expected output: 8
Python
Learn more from
Played 0 of 2 games
Sign in to save your progress and earn XP.