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: