Classes in the Standard Library
00:00 In the previous lesson, I showed you some more dunder methods and probably exhausted your eyes from all their rolling at my puns. In this lesson, I’ll show you some of the classes available to you in the Python standard library. Data classes were introduced in Python 3.7, and they’re a shortcut for creating the attributes on a class.
They’re kind of like a dictionary and a
namedtuple got together and had a wonderful little love child. You create a data class by wrapping a class declaration with the
@dataclass decorator. There are other ways as well if you dig into the docs, but I’m going to stick with this one.
00:37 In addition to allowing you to specify the names of attributes, you can indicate the type of the attribute in case you’re using type annotation tools. On top of all this, it is still a class, so you can add your own methods.
Now that Python 3.6 is no longer supported, it is safer to write code that assumes data classes exist. Increasingly, I’m writing a lot of my classes as data classes, so I don’t have to write the
.__init__() boilerplate. Let’s go look at some.
If you’re using a more recent version of Python, you can even use the
or-ed notation on the type. What the data class does for you is automatically implement a bunch of stuff. For free, you get a
.__init__() that uses the same arguments in the same order as they’re declared,
y in this case.
An enum is a grouping of constants. Note that the iterable utility
enumerate() existed in the language before enums were added, so don’t confuse the two concepts. And yes, you can enumerate an enumeration. Isn’t coding fun?
The idea of an enum is built into some programming languages. Python added it in 3.4, but instead of creating a new keyword, they used class magic, some very black meta class magic in fact, but you don’t need to know that to use them. To write an enum, you inherit from the
Enum class, and kind of like a data class, you declare the constants as class members. And it’s still a class, so you can add methods and do other classy things.
04:25 If you dig into the module, there are other ways to automatically assign values and shortcuts, but I’ll let you explore that on your own. With my enum class in hand, I of course can get at the attributes.
04:50 The purpose of an enum is grouping constants, so assigning a value appropriately gives an error. You can cast an enum to a list, and that list will contain all of its instances. You can construct the class with a value, and you’ll get back the corresponding enum object.
05:20 Remember when I said there was a reason you get back an enum object instead of just the value? Well, it’s because you can get at all the information associated with each part of the enum. Let me show you what I mean.
06:04 You may not have ever stopped and thought about what happens when you call a function, but in Python’s syntax, that parentheses on the end of the function are what is actually causing it to be called. But as everything is an object, even functions, using those parentheses to call something can be applied to an object as well. And in case you might have guessed, yep, there’s a dunder method for that.
When you stick parentheses on the end of an object, you’re calling it, and Python invokes the
.__call__() method for you. A neat little aspect of this is many of those things you thought were functions are not. All the conversion calls? Yep, not functions.
They’re classes that have implemented
.__call__(). In part one of this course, I spoke about using class methods for factories. These are essentially doing the same thing using that
07:09 I hope you’re with me and not feeling dunder the weather. Told you I can do this all day. Next up, let’s be lazy and partially implement some classes, letting someone else finish the job for us.
Become a Member to join the conversation.