
This is necessary (for example) if you want to use your class’ objects as dictionary keys. The last two options determine whether or not your object can be hashed. If you set this to true, you can sort a list of your objects. order actually creates four dunder methods, which determine the behavior for all lesser than and/or more than checks.eq does the same for the _eq_ dunder method, which determines the behavior for equality checks ( your_class_instance = another_instance).repr determines whether to generate the _repr_ dunder method.init determines whether to generate the _init_ dunder method.The default options repr=True, eq=True, order=False, unsafe_hash=False, frozen=False) More goodiesīy using options with the decorator, you can tailor data classes further for your use case.

Let’s see what a _post_init_ method would look like for our wrapper class:Īnd that’s it! We could implement our class using the data class decorator in under a third of the number of lines as we could without the data class decorator. Therefore a new dunder method was defined for any additional processing: _post_init_.

If we were to override _init_, we’d lose the benefit of the data class. Our Python 3.6 constructor didn’t just define all values, but it also attempted to parse dates. Unfortunately, we lost a bit of functionality in the process. We removed the _init_ method here to make sure the data class decorator can add the one it generates. Therefore, you get four dunder methods implemented just by changing the code StarWarsMovie: Python by default (not just for data classes) will implement _str_ to return the output of _repr_ if you’ve defined _repr_ but not _str_. If we don’t specify any options to the dataclass decorator, the generated methods are: _init_, _eq_, and _repr_. So let’s have a look at what we need to change.įirstly, data classes automatically generate several dunder methods. We’re creating a class that mostly holds data, and only does a little validation. This is a classic case of where the data class decorator can help you out. Not so careful readers may want to have a look at the complete Python 3.6 implementation : it’s not short. Self.edited = (self.edited)Ĭareful readers may have noticed a little bit of duplicated code here. So let’s define an object in Python 3.6 to contain the responses of requests to the /films/ endpoint: To properly wrap an API, we should create objects that our wrapper’s user can use in their application. 'opening_crawl': 'It is a period of civil war.\r\n … ', Requests makes our life easier by offering JSON parsing:Īnd at this point we have our data in a dictionary. This endpoint (like all swapi endpoints) responds with a JSON message. We can use the requests library to get a resource from the Star Wars API: Let’s forget for a second that there’s already a wrapper out there, and see how we could write our own. He’s actually gone even further, and created a Python wrapper library for it. One Star Wars fan has done exactly that, and created the Star Wars API. You know a movie’s fanbase is passionate when a fan creates a REST API with the movie’s data in it. So let’s have a look at how we can use this! The Star Wars API At runtime, these types are not checked, but you can use P圜harm or a command-line tool like mypy to check your code statically. If you’ve never used a type hint before: they allow you to mark what type a certain variable should be. The small example before could be rewritten M圜lass:Ī key difference is that type hints are actually required for data classes. For example, a init which accepted those arguments and assigned each to self. Most Python developers will have written many classes which looks like:ĭata classes help you by automatically generating dunder methods for simple cases. One of the most exciting new features is support for the dataclass decorator. There are many new things in Python 3.7: various character set improvements, postponed evaluation of annotations, and more.

Python 3.7 is set to be released this summer, let’s have a sneak peek at some of the new features! If you’d like to play along at home with P圜harm, make sure you get P圜harm 2018.1 (or later if you’re reading this from the future).
