Search⌘ K
AI Features

Creating a Class

Explore how to create classes in Python, including defining attributes and methods, understanding the __init__ constructor, and using self. This lesson helps you build foundational skills in object-oriented programming to write and instantiate effective Python classes.

We'll cover the following...

Creating a class in Python is very easy. Here is a very simple example:

Python
# Python 2.x syntax
class Vehicle(object):
"""docstring"""
def __init__(self):
"""Constructor"""
pass

This class doesn’t do anything in particular, however it is a very good learning tool. For example, to create a class, we need to use Python’s class keyword, followed by the name of the class. In Python, convention says that the class name should have the first letter capitalized. Next we have an open parentheses followed by the word object ...