How to Create Custom Data Types in Python
When you first learn Python, you use the basic built-in data types. You use str for text, int for whole numbers, and list to hold multiple items. These basic types are great for simple programs.
But as your programs grow larger, you will need to group different pieces of information together.
To make your code safer and easier to read, you can create your own custom data types. By doing this, you tell Python exactly what information is allowed and how it should behave.
When to Use Custom Types
You do not always need custom data types. Knowing when to use the built-in types (like lists and dictionaries) saves you a lot of time.
Use Built-in Data Types When:
- You are writing a quick, short script.
- The data is simple and temporary.
- Example: If you just need to read a text file and count how many times the word "Python" appears, you only need built-in strings (
str) and numbers (int).
Use Custom Data Types When:
- You are building a large application that you will maintain for a long time.
- Specific pieces of data always travel together.
- The data has strict rules.
- Example: If you are building an email application, you should create a custom
Emaildata type. An email must always have a sender, a receiver, a subject line, and a true/false value showing if it has been read. If you just use a dictionary, another programmer might forget to include the "sender" information, which would break the application. A custom type prevents this mistake.
Here are three ways to create custom data types in Python, starting with the most common way.
1. Using Standard Classes (For Data and Actions)
In Python, the most common way to create a custom data type is by using a class.
You can think of a class as a strict blueprint. It tells Python exactly what pieces of information belong together. It also lets you create actions that the data can perform.
2. Using Data Classes (For Just Holding Information)
Writing the setup function (the __init__ part in the code above) can take a lot of time if you have 10 or 20 pieces of information to store.
If you want to create a custom data type that only holds information and does not need to perform actions, you should use a Data Class. Python added Data Classes to write all the repetitive setup code for you behind the scenes.
3. Using Enums (For a Strict List of Choices)
Sometimes, a piece of data should only be allowed to be one of a few exact choices.
For example, an order status in a store application should only be "Pending", "Shipped", or "Delivered". If you use regular text strings for this, someone might accidentally type "Shiped" with one "p".
To prevent these spelling mistakes, you can use an Enum. This simply means a numbered list of specific choices.
Summary
Here is a simple guide to help you choose which method to use:
| What do you need your custom data type to do? | What should you use? |
|---|---|
| It needs to hold information and perform specific actions. | Use a standard class. |
| It only needs to hold information. | Use a @dataclass. |
| It needs to restrict information to a specific list of choices. | Use an Enum. |
FAQs
?Why do I have to write "self" so many times in standard classes?↓
?Can I put a custom data type inside another custom data type?↓
?Should I stop using dictionaries completely?↓
?Do custom data types slow down my program?↓
ls ./related-articles
Python List vs Tuples
Choosing between lists and tuples in Python directly affects your code's performance, memory usage, and reliability. This guide explains the technical differences and when to use each data structure.
How to Install Python on Windows, macOS, and Linux
This guide shows how to install Python in Windows, Mac and Linux OS. It also, shows the most common errors come while installing python and how to fix that.