Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python dataclasses

Alex Kiura
October 26, 2018

Python dataclasses

A primer on python dataclasses

Alex Kiura

October 26, 2018
Tweet

Other Decks in Programming

Transcript

  1. What are data classes? • Introduced in python 3.7 •

    First and foremost Python classes • Automatically generate special methods for classes • Useful for classes which store multiple properties • Can be used as a mutable data holder • Created using the data_classes decorator from dataclasses import dataclass @dataclass class Person: name: str age: int
  2. Storing data in a class: • Using normal python classes

    • Using named tuples • Using data classes
  3. Why not use normal classes? class Person: def __init__(self, name,

    age): self.name = name self.age = age def __repr__(self): return 'Person(name={0}, age={1})'.format(self.name, self.age) >>> person Person(name=Jane, age=21)
  4. Why not use named tuples? from collections import namedtuple Person

    = namedtuple('Person', ['name', 'age']) person = Person(name='Jane', age=21) >>> print(person) Person(name='Jane', age=21)
  5. Creating data classes • Use the dataclass decorator • The

    decorator examines the class to get field • A field is a class variable with a type annotation from dataclasses import dataclass @dataclass class Person: pass Initializing data classes
  6. Creating data classes • Fields are specified as class variables

    with type annotations • A type hint MUST be provided, or else the field will be ignored • typing.Any can be used to avoid explicitly stating a type from dataclasses import dataclass from typing import Any @dataclass class Person: name: str age: int gender: str id_number: Any Adding fields and type hinting
  7. Creating data classes •Default values can also be specified •This

    is equivalent to specifying default values in the __init__ method from dataclasses import dataclass @dataclass class Person: name: str age: int gender: str marrital_status: str = 'Single' citizenship: str = ‘Kenyan' >>> person = Person(name='Jane', age=21, gender=‘Female’) >>> person.marrital_status ‘Single’ >>> person.citizenship 'Kenyan' Adding default values
  8. Creating data classes •Since data classes are still Python classes,

    we can extend the functionality by adding functions. from dataclasses import dataclass @dataclass class Person: name: str age: int gender: str marrital_status: str = 'Single' citizenship: str = 'Kenyan' @property def is_minor(self): return self.age < 18 >>> person = Person(name='Jane', age=21, gender='Female') >>> person.is_minor False Adding functions
  9. Creating data classes • Data classes are mutable by nature

    which means the values of the fields can be modified • However data classes can be modified to be immutable (similar to namedtuples) • A data class is made immutable by passing frozen=True to the @dataclasses decorator from dataclasses import dataclass # Mutable @dataclass class Person: name: str age: int gender: str >>> person = Person(name='Jane', age=21, gender='Female') >>> person.age 21 >>> person.age = 24 >>> person.age 24 # Immutable @dataclass(frozen=True) class Person: name: str age: int gender: str >>> person.age =24 Traceback (most recent call last): File "<input>", line 1, in <module> person.age =24 File "<string>", line 3, in __setattr__ dataclasses.FrozenInstanceError: cannot assign to field 'age' Mutability
  10. Creating data classes • It is possible to generate a

    dictionary based on a data class and the defined fields. • We use dataclasses.asdict to get a dict representation of the data class. from dataclasses import dataclass from dataclasses import asdict @dataclass class Person: name: str age: int gender: str >>> person = Person(name='Jane', age=21, gender='Female') >>> asdict(person) {'name': 'Jane', 'age': 21, 'gender': 'Female'} To dict
  11. Why use data classes • Automatically adds special methods: •

    __init__ • __repr__ • __eq__ • __str__ • Adds type hinting • Short learning curve • asdict and astuple to represent data in different formats.