Dataclasses: The code generator to end all code generators - Deepstash
Dataclasses:  The code generator to end all code generators

Dataclasses: The code generator to end all code generators

Curated from: PyCon 2018

Ideas, facts & insights covering these topics:

6 ideas

·

182 reads

5

Explore the World's Best Ideas

Join today and uncover 100+ curated journeys from 50+ topics. Unlock access to our mobile app with extensive features.

Dataclasses & What are they for

Dataclasses & What are they for

Dataclasses are an implementation of a code generator, similar to a Named Tuple.

The idea of using a code generator is to save time and reduce wordiness.

There are two views about the purpose of Dataclasses:

  1. It makes a mutable data holder, in the spirit of named tuples
  2. It writes boiler-plate code for you, simplifying the process of writing the class

These 2 world views are reflected in the name: “Dataclasses”

Per the dataclasses PEP, they are roughly a “Mutable named tuple with defaults”

They provide an elegant syntax for creating data holder objects

5

40 reads

A History Lesson

A History Lesson

In the beginning there where:

  • dicts for look-ups
  • tuples for structures
  • hand-written classes for more functionality

Later Named Tuples where added as a code generator with the goal of adding names to the fields of a tuple. This was limited by design, hence its name

ORMs appeared and pioneered using class attributes to specify rich data structures

In Python 3.6, it became possible to supply type annotations

5

28 reads

Comparison with Named Tuples

Comparison with Named Tuples

  • Dataclasses are by default mutable
  • Named Tuples have methods which start with an underscore which can be confusing. Ex: obj._replace(f=value)
  • Named Tuples have legacy code. Ex: _asdict() returns an OrderedDict
  • Named Tuples can be unpacked
  • Dataclasses are faster at retrieving data but take up more space
  • Dataclasses use functions instead of methods. Ex: asdict() vs nt._asdict()
  • Dataclasses are underlying stored as instance dicts

5

21 reads

RAYMOND HETTINGER

If you have an immutable mind get out!

RAYMOND HETTINGER

5

40 reads

Generated Code Includes

  • Dunder methods like init, repr, eq, lt, le, etc.
  • Frozen nature by generating dunder methods for __setattr__ and __delattr__
  • Class variables for the provided attributes

The code generation can be configured by providing keyword arguments to the @dataclass decorator

A complete list of methods and configurations can be found here

5

21 reads

A Complex Example of using Dataclasses

A Complex Example of using Dataclasses

In the image example, the generated code includes:

  • typed attributes
  • hidden attributes from __repr__ response
  • custom __hash__ dunder with the selected fields
  • attribute with default factory value in __init__ constructor

5

32 reads

IDEAS CURATED BY

pvl

Ex Backend @deepstash

CURATOR'S NOTE

I started using Dataclasses and I think you should too

Pavel-Vlad Mateescu's ideas are part of this journey:

Machine Learning With Google

Learn more about computerscience with this collection

Understanding machine learning models

Improving data analysis and decision-making

How Google uses logic in machine learning

Related collections

Read & Learn

20x Faster

without
deepstash

with
deepstash

with

deepstash

Personalized microlearning

100+ Learning Journeys

Access to 200,000+ ideas

Access to the mobile app

Unlimited idea saving

Unlimited history

Unlimited listening to ideas

Downloading & offline access

Supercharge your mind with one idea per day

Enter your email and spend 1 minute every day to learn something new.

Email

I agree to receive email updates