Start learning free

LESSON 176 OF 344 · OBJECT-ORIENTED PROGRAMMING

Defining a Class in Python: class, pass and PascalCase

Lesson Read the full explanation

TL ; DR:

  • A class is a blueprint for creating objects. It groups together related data and behavior under a single name.


  • In Python, you define a class with the class keyword, followed by the class name, a colon, and an indented body:

    class Dog:
        pass
    

    The body must contain something — Python uses pass as a placeholder when there's nothing to do yet. We'll fill it in over the next lessons.



Full lesson:

So far, you've worked with built-in types like int, str, and lists. But what if you want to model something Python doesn't have a type for — like a Dog, a BankAccount, or a Game?

That's what classes are for. A class lets you define your own type by listing what data it holds and what actions it can perform.


Why classes?

Imagine you're writing a program about dogs. Each dog has a name, an age, and can bark. Without classes, you'd have to track each dog's name in one variable, age in another, and write a separate bark() function for each. That gets messy fast.

With a class, you bundle all of that into one neat package called Dog:

class Dog:
    pass   # We'll add attributes and methods in upcoming lessons.

The syntax

Defining an empty class in Python looks like this:

class ClassName:
    pass

Three things to notice:

  • The keyword class tells Python you're defining a new type.

  • ClassName is the name you choose. By convention, Python class names use PascalCase — capitalized words with no underscores: Dog, BankAccount, HighScore.

  • A colon ends the class header, and the body is indented below. Even an empty class needs something in its body — that's what pass is for: a do-nothing statement that's syntactically valid.


Your task

Define an empty class called Dog. The pre-filled code below will create an instance of your class and print a confirmation message. If you define the class correctly, the program will run and print:

Defined the Dog class!
STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · OBJECT-ORIENTED PROGRAMMING

    Defining A Class in Python, explained

    What a class is

    Python comes with types such as int, str and list. A class lets you define a type of your own. Think of it as a blueprint: it describes what a thing is, and later you build actual things from it. A Dog, a BankAccount or a Game are all natural candidates, because each one groups some data with the actions that go with it.

    Without classes, a program about dogs ends up with one variable for each dog's name, another for each age, and loose functions that take those variables as arguments. A class bundles all of that under a single name, so the program can talk about "a dog" instead of three unrelated pieces.

    This lesson covers only the first step: writing the class itself. Data and behavior are added in the next lessons.

    The syntax

    An empty class looks like this:

    class ClassName:
        pass
    

    Three details matter:

    • The keyword class tells Python that a new type is being defined.
    • The name comes next. Python programmers write class names in PascalCase: every word starts with a capital letter and there are no underscores, as in Dog, BankAccount or HighScore. Functions and variables use lowercase with underscores instead, so the capital letter tells a reader at a glance that a name is a class.
    • A colon ends the header line, and the body is indented below it. Python uses indentation, not braces, to decide what belongs to the class.

    Why the body needs pass

    A class body cannot be empty. If you write the header and nothing below it, Python reports a syntax error because it expects an indented block. The statement pass does nothing at all, but it is a valid statement, so it fills the block. It is a placeholder that says "nothing here yet".

    A docstring, a string literal on its own as the first line of the body, would also make the block valid. In this lesson pass is the simplest choice.

    The lesson's exercise

    The task is to define an empty class named Dog. The code below the class is already written: it creates one instance of the class and prints a confirmation. If the class does not exist, the line that creates the instance fails with a NameError, so the program never reaches the message.

    1. Write the header. Type class Dog: with a capital D and the colon at the end. The name must match exactly, because the code below refers to Dog.
    2. Indent the body. On the next line, indent by four spaces and write pass.
    3. Leave the rest alone. Calling Dog() is covered in the next lesson. For now, you only need to know that it builds an object from your class.

    The finished program:

    class Dog:
        pass
    
    dog = Dog()
    print("Defined the Dog class!")
    

    It prints:

    Defined the Dog class!
    

    What an empty class can already do

    Even with only pass in it, the class is a real type. You can create objects from it, check their type, and compare them:

    class Dog:
        pass
    
    a = Dog()
    b = Dog()
    print(isinstance(a, Dog))
    print(a is b)
    
    True
    False
    

    Each call to Dog() produces a separate object, so a is b is False. An empty class has no data and no behavior of its own, which is why the following lessons add attributes and methods to it.

    Common mistakes

    • Forgetting the colon. class Dog without the : is a syntax error.
    • Forgetting the indented body. A header with nothing under it is also a syntax error. Add pass.
    • Using the wrong capitalization. dog and Dog are different names in Python. A class named dog works, but it breaks the convention and the lesson's code expects Dog.
    • Unindented pass. If pass sits at the left margin, it is outside the class and the class has no body.

    Key takeaways

    • A class is a blueprint for a new type, defined with the class keyword.
    • The header is the name, in PascalCase, followed by a colon. The body is indented.
    • The body cannot be empty, and pass is the placeholder that makes it valid.
    • Calling the class, as in Dog(), creates an object from it.
    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free