2) Classes, Objects, and Methods Lesson

How to Create a Python Class

6 min to complete · By Martin Breuss

In the previous lesson, you've created your first custom class. In this lesson, you'll revisit and learn some more about the syntax necessary to create a class in Python.

Define a Class

Similarly to a Python function, you define a class with a specific keyword. While you use def for a function, you use class for a class:

class ClassName:
    """Docstring"""
    pass

Class Name

After the class keyword follows the name of your class, usually in singular. Classes in Python are named following the CapCase convention. As you might realize, the reason why you shouldn't capitalize any of your function and variable names is so that you'll quickly know whether or not you're working with a class.

CapCase in Python should only be used when defining classes since this convention gives developers a standard way to identify them when reading Python code.

Docstring

Following the colon (:) that ends the head of your class definition, you can optionally add a docstring. Just like with your functions, it's recommended to add a docstring because it'll make it much easier for your future self and other developers to work with your classes.

Indentation

Finally, all the indented code following your docstring will make up the content of your class. In this first example, your class is empty, which is why the definition only includes the pass keyword, which does nothing.

When you are building out your custom classes later on in this section, you'll write attributes and methods here instead.

Instantiate a Class

As you've learned earlier in this section, classes can be thought of as blueprints for Python objects. You'll use their information to instantiate an object, also called an instance of that class.

How to Instantiate a Class

You can do this using the same syntax as when calling a function:

i = Ingredient()

This line of code instantiates an Ingredient object and saves it to the variable i. This syntax might look familiar from other code you wrote earlier:

d = dict()

What is Instantiating a Class

Remember how everything is an object in Python? Indeed, when you write the line of code shown above, you're instantiating an empty dict object, just like you instantiated an empty Ingredient object with the line of code shown further up.

Built-in types in Python, such as dict, are also just objects. You'll notice that the most common built-in types are written in lowercase instead of CapCase. This is mostly for historical reasons, so remember to always write your own custom class names in CapCase.

Coming up, you'll loop back to your empty Ingredient class and experiment with what you can do with it without even defining any additional logic in the class.

Colorful illustration of a light bulb

Additional Resources

Summary: How to Create a Python Class

  • A constructor is used to instantiate an object from a class
  • The name of the class followed by parentheses is the constructor
  • The output of a constructor can be set to a variable

Class Definition

  • the class keyword
  • the name of your class in CapCase and singular
  • a colon (:)
  • the docstring of the class, indented by four spaces
  • the body of your class definition containing all the code that makes up your class. This code also needs to be indented by four spaces.

Syntax

The code snippet below provides you with the syntax for creating a class in Python.

class ClassName:
    """Docstring"""
    pass