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 PyCharm, make sure you get PyCharm 2018.1 (or later if you’re reading this from the future).
There are many new things in Python 3.7: various character set improvements, postponed evaluation of annotations, and more. One of the most exciting new features is support for the dataclass
decorator.
What is a Data Class?
Most Python developers will have written many classes which looks like:
1
2
3
4
|
class MyClass:
def __init__(self, var_a, var_b):
self.var_a = var_a
self.var_b = var_b
|
Data classes help you by automatically generating dunder methods for simple cases. For example, a __init__ which accepted those arguments and assigned each to self. The small example before could be rewritten like:
1
2
3
4
|
@dataclass
class MyClass:
var_a: str
var_b: str
|
Source: Python 3.7: Introducing Data Classes | PyCharm Blog