If you’d like to learn more about these examples, then check out:
Exploring Multiple Constructors in Existing Python Classes
Exploring Multiple Constructors in Existing Classes. The
@classmethod decorator is used in several built-in and standard-library classes to provide multiple alternative constructors. In this section, you’ll learn about three of the most notable examples of these classes:
00:25 Dictionaries are a fundamental data type in Python. They’re present in every piece of Python code, either explicitly or implicitly. They’re also a cornerstone of the language itself because important parts of the CPython implementation rely on them.
This class method takes an iterable of keys and an optional
value argument defaults to
None and works as the value for all the keys in the resulting dictionary. Now, how can
.fromkeys() be useful in your code?
01:18 Let’s say you are running an animal shelter, and you need to build a small application to track how many animals currently live there. Your app uses a dictionary to store the inventory of animals.
In this example, you build an initial dictionary using
.fromkeys(), which takes the keys from
allowed_animals. You set the initial inventory of each animal to
0 by providing this value as the second argument to
As you already learned,
value defaults to
None, which can be a suitable initial value for your dictionary’s keys in some situations. However, in this example,
0 is a convenient value because you are working with the number of individuals that you have from each species.
The method creates a new dictionary by calling
cls. Then it iterates over the keys in
iterable and sets each value to
value, which defaults to
None as usual. Finally, the method returns the newly created dictionary.
datetime.date class from the standard library is another class that takes advantage of multiple constructors. This class provides several alternative constructors, such as
All of them allow you to construct
datetime.date objects using conceptually different arguments. On-screen, you’ll see a few examples of how to use some of those constructors to create
The rest of the examples show how
datetime.date uses several class methods to provide multiple constructors. This variety of constructors makes the instantiation process pretty versatile and powerful, covering a wide range of use cases.
pathlib module from the standard library provides convenient and modern tools for gracefully handling system paths in your code. If you’ve never used this module, then check out Python 3’s pathlib Module: Taming the File System.
The handiest tool in
pathlib is its
Path class. This class allows you to handle your system path in a cross-platform way.
Path is another standard-library class that provides multiple constructors, in this case
.home() constructor returns a new path object representing the user’s home directory. This alternate construction can be useful when you’re handling configuration files in your Python applications and projects.
Become a Member to join the conversation.