Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Python provides many ways to manage program complexity. Rather than creating variables to represent every single element, elements can be organized into a hierarchy of lists and dictionaries. Rather than copy-pasting similar pieces of code across your project, program logic can be organized into functions to enable reuse. But what if you need to organize data and program logic into a single unit? Objects combine data structures with your own functions that can operate on them.

We define a class using the class keyword. By convention, class names are capitalized with no underscores in between each word.

class ClassName:
    class_var = 42

    def __init__(self, parameter):
        self.instance_var = parameter

    # ... other methods ...

Creating an instance of a class is called instantiation.

my_object = ClassName(18)

Class and instance variables (aka attributes or fields) can be accessed using dot notation.

my_object.instance_var
my_object.class_var
ClassName.class_var

Practice: Pokemon

Consider this Pokemon class.

class Pokemon:
    hp = 20  # Class variable shared by all Pokemon instances

    def __init__(self, name, kind):
        self.name = name     # Instance variable
        self.type = kind     # Instance variable


# Creating specific Pokemon instances
charmander = Pokemon("Charmander", "Fire")
piplup = Pokemon("Piplup", "Water")

Does charmander.hp += 40 work? What is piplup.hp afterwards?

charmander.hp += 40
piplup.hp

Practice: Evolving Pokemon

Pokemon can evolve!

class Pokemon:
    hp = 20

    def __init__(self, name):
        self.name = name

    def evolve(self):
        self.hp *= 2

    def evolve_all(self):
        Pokemon.hp *= 3

eevee = Pokemon("Eevee")
bulbasaur = Pokemon("Bulbasaur")

eevee.evolve()
eevee.evolve_all()

What is the value of bulbasaur.hp afterwards?

Practice: University

Define a University class with the following attributes and methods:

Nested objects

Just as we can have nested structures, objects can also contain other objects! For example, instead of storing a list of strings for the departments in our University class, we could populate it with a list of Department objects:

class Department:
    def __init__(self, name):
        self.name = name


cse = Department("Computer Science & Engineering")
bio = Department("Biology")
info = Department("School of Information")

# Our depts list now stores custom objects instead of strings
uw.depts = [cse, bio, info]

Dunder Methods

Dunder (double underscore) methods serve special purposes in Python.

How would you add dunder methods to each of the above classes?