Classes

Classes are templates used to define the properties and methods of objects in code. They can describe the kinds of information that the class holds, and also how a programmer interacts with that data.

It serves as a blueprint or template for the objects, or “instances” of the class.

Creating a Class

In Python, classes are defined using the class keyword.

Loading...

Class definitions cannot be empty, so the pass statement can be used as a placeholder to avoid errors.

Loading...

Creating Instances of a Class

Objects can be created from classes. These objects are called instances of a class, and when we create an instance, that is known as instantiating a class.

To create an instance of a class, we can set a variable equal to the class name followed by parentheses ().

Loading...

Here, the instance name is my_home, and the class is Home.

Class Methods

Methods are functions defined as part of a class. The first parameter for any class method is the actual object calling the method, usually called self.

For example, the following class Home has a method called .paint_wall():

  • The first parameter is self.
  • The second parameter is color.

When calling a class method, no argument is provided for the parameter self, but arguments must be provided for each following parameter.

Loading...

Class Attributes

Class attributes are variables that are defined outside of all methods and have the same value for every instance of the class.

Loading...

Instance Variables

Instance variables are variables that are unique to each instance of a class. They can be set initially within the __init__() method when the instance is created, or instance variables can be added by setting them to values.

Loading...

We give the instance dog a new instance variable called name and set it to the value "Ruff".

init() Method

This method is used to initialize a newly created object. It is called each time a class is instantiated. Instance variables are set within the __init__() method block. Input parameters can be set for this method, which are passed during instantiation.

Loading...

repr() Method

The __repr__() method returns the string representation of the class. One way to see the string representation is to call print() on the instance.

Loading...

Accessing Methods and Variables in the Class

Within the code of the class, we can call its methods and access its variables. We do this by using self followed by a period . and then followed by the method or variable.

Loading...

type() Function

The type() function returns the data type of the argument passed to it. When the argument is an instance of a class, it returns the class that it is an instance of.

Loading...

hasattr() Function

The hasattr() function can be used to check if an instance of a class has an attribute. It returns True if it does have the attribute, and False otherwise.

Loading...

Deleting Objects

Objects, or instances of classes, can be deleted using the del keyword.

Loading...

Deleting Attributes

Attributes of objects can be deleted using the del keyword.

Loading...

Example

Lets demonstrate a class for an Employee:

Loading...

Once we create an Employee class like we did above we can import the module in our code and create instances of it whenever and wherever we want, thereby enabling code reusablity and making our code more efficient and easier to maintain.

Loading...

When creating applications classes are a great way to organize and improve the quality of your code.

Interested in contributing to Codecademy Docs? Check out our Contribution Guide or leave us some feedback.

Learn Python on Codecademy