What is "object-state" in Python?


Let's imagine that we have a toy box. The toy box is like a class in Python. It's a special kind of box that knows all about the toys you can put inside it and what you can do with those toys. Now, let’s say you have a magic toy inside this box—a talking book that can remember the page number you are on, even if you close it and open it again later. That's like a method in the class—it's something special the toy can do. When you play with the talking book, and you turn its pages, the book remembers the page number. That page number is like the object's state. It's a bit like remembering which part of a story you are reading. If you close the book and come back later, you can start where you left off without starting over from the beginning. That’s because the book “remembers” by keeping track of the page. If we didn’t have this special toy box (the class) and the magic book (the object with methods and state), we’d have to remember the page number ourselves, or we’d always have to start reading from the first page every time, which wouldn’t be as fun. So, using a class in Python helps us keep track of important things like our page number so we don’t have to remember it all by ourselves, making everything easier and more fun!

Let's make a simple Python example to explain object state bit deeper. We'll create a little program with a class that represents a balloon. The balloon can inflate and deflate, and it remembers its size—that's its state!


Example: Balloon Class

Imagine a balloon that you can inflate (blow air into) and deflate (let air out). The size of the balloon is its state, and we'll see how it changes:

class Balloon:
def __init__(self):
self.size = 0 # Size of the balloon starts at 0 (deflated)

def inflate(self):
self.size += 1 # Each time you inflate, the balloon gets bigger
print(f"The balloon size is now {self.size}")

def deflate(self):
if self.size > 0:
self.size -= 1 # Each time you deflate, the balloon gets smaller
print(f"The balloon size is now {self.size}")

def show_size(self):
print(f"The current balloon size is {self.size}") # Create a new Balloon
my_balloon = Balloon() # Inflate the balloon a few times
my_balloon.inflate() # The balloon size is now 1
my_balloon.inflate() # The balloon size is now 2
my_balloon.inflate() # The balloon size is now 3 # Show the balloon size
my_balloon.show_size() # The current balloon size is 3 # Deflate the balloon
my_balloon.deflate() # The balloon size is now 2
my_balloon.deflate() # The balloon size is now 1 # Show the balloon size again
my_balloon.show_size() # The current balloon size is 1

Here, the size attribute keeps track of the balloon's state, which changes with each inflate and deflate action. This example demonstrates how the object (balloon) maintains its state (size) throughout its life in the program.

Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.