Remember
A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods.
A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Counter:
def __init__(self, value):
self.value = value
def increment(self):
self.value += 1
counter = Counter(3)
counter.increment()
print(counter.value)Top-level scope
class CounterObject 3 · CounterCounter({'value': 4})A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Counter:
def __init__(self, value):
self.value = value
def increment(self):
self.value += 1
counter = Counter(3)
counter.increment()
print(counter.value)
|
v
DISPLAYED RESULT
4
FINAL BINDINGS
Counter --> object 2 (type)
counter --> object 3 (Counter)
object 2: class Counter
object 3: Counter({'value': 4})Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Counter:
def __init__(self, value):
self.value = value
def increment(self):
self.value += 1
counter = Counter(3)
counter.increment()
print(counter.value)Top-level scope
class CounterObject 3 · CounterCounter({'value': 4})The example has finished. Compare the final output with the values and references above.
4
What do you expect this program to print?
class Counter:
def __init__(self, value):
self.value = value
def increment(self):
self.value += 1
counter = Counter(3)
counter.increment()
print(counter.value)When writing programs, we often deal with real-world things like students, cars, or bank accounts. These things have data (like a student's name or a car's color) and actions (like driving a car or updating a bank balance).
Object-Oriented Programming (OOP) helps us model these things in code using objects and classes.
Imagine you want to create a Dog. Every dog has a name and can bark. name is the attribute of a dog, while barking is its behaviour(method).
# Defining a class
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(self.name + " says Woof!")
# Creating objects
dog1 = Dog("Tommy")
dog2 = Dog("Buddy")
# Calling methods
dog1.bark()
dog2.bark()
Tommy says Woof!
Buddy says Woof!
class Dog: creates a class named Dog.__init__() is a special method that runs when we create an object. It sets the dog’s name.self.name stores the name of the dog.bark() is a method that prints a message using the dog’s name.dog1 = Dog("Tommy") creates a dog named Tommy.dog1.bark() tells Tommy to bark!We’ll make our Dog learn a new trick: sit.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(self.name + " says Woof!")
def sit(self):
print(self.name + " sits down.")
dog = Dog("Charlie")
dog.bark()
dog.sit()
Charlie says Woof!
Charlie sits down.
self is used to refer to the current object. It lets each object keep its own data separate from other objects.
Connect what you noticed in the example with the idea below.
A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods.
Every instance must share the same value for every attribute.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.