From 6c3224f9513f7df5e8df36cc7f164ef44658b7b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sat, 13 Apr 2019 17:11:09 +0200 Subject: [PATCH] Dataclass --- README.md | 8 ++++---- index.html | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 52d4829..84aed04 100644 --- a/README.md +++ b/README.md @@ -670,7 +670,7 @@ from functools import reduce ['zero', 1, 'zero', 3] ``` -### Namedtuple, Enum, Class +### Namedtuple, Enum, Dataclass ```python from collections import namedtuple Point = namedtuple('Point', 'x y') @@ -684,9 +684,9 @@ Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3}) ``` ```python -# Warning: Objects will share the objects that are initialized in the dictionary! -Creature = type('Creature', (), {'p': Point(0, 0), 'd': Direction.n}) -creature = Creature() +from dataclasses import make_dataclass +Creature = make_dataclass('Creature', ['location', 'direction']) +creature = Creature(Point(0, 0), Direction.n) ``` diff --git a/index.html b/index.html index e384294..25778af 100644 --- a/index.html +++ b/index.html @@ -620,7 +620,7 @@ func(*args, **kwargs)
>>> [a if a else 'zero' for a in (0, 1, 0, 3)]
 ['zero', 1, 'zero', 3]
 
-

Namedtuple, Enum, Class

+

Namedtuple, Enum, Dataclass

from collections import namedtuple
 Point     = namedtuple('Point', 'x y')
 point     = Point(0, 0)
@@ -629,9 +629,9 @@ point     = Point(0, 
 Direction = Enum('Direction', 'n e s w')
 Cutlery   = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3})
 
-
# Warning: Objects will share the objects that are initialized in the dictionary!
-Creature  = type('Creature', (), {'p': Point(0, 0), 'd': Direction.n})
-creature  = Creature()
+
from dataclasses import make_dataclass
+Creature  = make_dataclass('Creature', ['location', 'direction'])
+creature  = Creature(Point(0, 0), Direction.n)
 

#Closure

We have a closure in Python when: