GCGrowCarrer

Classes and Objects

Locked

Lesson 16 of 60 · OOP · Medium

Finish these first:Functions →Or pass the tests below to unlock it now.

1

The big idea

A class bundles data (attributes) and behaviour (methods). self is the instance.

Think of it like this: Think of a Class as a cookie cutter (the blueprint) and each Object as an individual cookie stamped from it — each cookie can have its own frosting (state) while sharing the same shape (methods).
Key words (tap to learn):
2

Read the lesson

A class bundles data (attributes) and behaviour (methods). self is the instance.

class Account:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance
    def deposit(self, amt):
        self.balance += amt

Class attributes are shared; instance attributes belong to each object.

3

Quick check

Before writing code: What should `(lambda s: (s.push(1), s.push(2), s.pop(), s.size())[-2:])(Stack())` return when your implementation of Classes and Objects is working?

4

Your challenge

2 sample tests, 1 hidden

Implement class Stack with push(x), pop(), and size().

Stuck? Get a hint (free)
More learning tools(compare languages, visual guide, related lessons)

Compare with another language

Similar to java.classes-objects (80% overlap). Explicit `self` in Python vs implicit `this` in Java; Java fields are declared.

Visual walkthrough

A diagram, a step-by-step animation, and short captions for Classes and Objects.

Mastery0%