Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Apr 26, 2021Updated May 17, 2024
Contribute to Docs

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

The usage of classes are a key element of object-oriented programming (OOP) where “class instances” are created to implement design principles such as inheritance and encapsulation.

Creating a Class

In Python, classes are defined using the class keyword. The first letter of the name of the class is always capitalized. For instance, the first letter of the Home class in the below example, i.e., ‘H’, is capitalized:

class Home:
# Class body starts here

Note: The example above would actually be invalid because class definitions cannot be empty. However, the pass statement can be used as a placeholder to avoid errors:

class Home:

In the class body, the self keyword, followed by a period ., can be used to call its methods and access its variables, as shown in the __init__() dunder method below:

class Home:
def __init__(self, rooms, stories):
# Setting instance variables
self.rooms = rooms
self.stories = stories

Class Instances

Objects can be created or instantiated from classes. These objects are known as class instances and are created by setting a variable equal to the class name followed by parentheses ():

my_home = Home()

Here, the instance name is my_home, which derives from the Home class. Calling this line implicitly calls the Home class’s __init__() method.


Class attributes are variables that are defined outside of all methods and have the same value for every instance of the class. They also can be accessed via the class name rather than the instance name. Setting the variable via the class name will change it for all instances.

Note: Setting it via an instance name will break the connection for that instance.

class Bird:
# Class attribute
is_hungry = True
parakeet = Bird()
parrot = Bird()
print("Birds are hungry!")
print("Feeding birds...")
parakeet.is_hungry = False
parrot.is_hungry = False
print("Birds fed!")

This will output the following:

Birds are hungry!
Feeding birds...
Birds fed!


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

The following is a slight modification of the Bird class from the previous section:

class Bird:
# Class attribute
is_hungry = True
def feed_bird(self, food):
self.is_hungry = False
print(f"Feeding with {food}. Bird fed!")
print("Bird already ate.")
sparrow = Bird()

A feed_bird() method is defined in the Bird class body, accepting a food parameter. Inside, an if..else conditional prints a different message depending on whether or not the is_hungry attribute is true. The following will be printed based on the example:

Feeding with seeds. Bird fed!
Bird already ate.

Static Methods

It is also possible to create a method that only applies to the class itself, not instances of the class. These can be distinguished with @classmethod and @staticmethod:

class Home:
name="Code Ninja"
rooms = 4
stories = 2
def is_on_market(home):
if( == ""):
return True
return False
def paint_wall(self, color):
return f"Painting wall with the color {color}."
home = Home()
# Output: False


The following example demonstrates an Employee class defined in a file called

class Employee(object):
name = "Sam"
company = "ILoveCode Inc."
age = 30
is_on_vacation = True
def working(self, employee_name): = employee_name
print(f"{employee_name} is working")

Afterward, the Employee class can be imported into other files where new instances and methods can be created. This makes the code efficient, reusable, and maintainable:

from employee import Employee
def create_employee():
print("Employee is starting their job...")
employee1 = Employee() = "Blake"

Running the code in will output the following:

Employee is starting their job...
Blake is working

Codebyte Example

Defined methods and variables can be accessed within the class definition. This is done with the self keyword, followed by a period ., and then followed by the method or variable. In the example below, the defined .getName() method is used inside the .sayHi() method:

Visit us
Hide code
Hide output
Hide output

All contributors

Looking to contribute?

Learn Python on Codecademy