Warum kann ich nicht ein typing.NamedTuple
einlegen, während ich ein collections.namedtuple
beizen kann? Wie kann ich es schaffen, ein NamedTuple
Pickle zu machen?Warum kann ich nicht ein typing.NamedTuple pickle, während ich ein collections.namedtuple pickle?
Dieser Code zeigt, was ich bisher versucht habe:
from collections import namedtuple
from typing import NamedTuple
PersonTyping = NamedTuple('PersonTyping', [('firstname',str),('lastname',str)])
PersonCollections = namedtuple('PersonCollections', ['firstname','lastname'])
pt = PersonTyping("John","Smith")
pc = PersonCollections("John","Smith")
import pickle
import traceback
try:
with open('personTyping.pkl', 'wb') as f:
pickle.dump(pt, f)
except:
traceback.print_exc()
try:
with open('personCollections.pkl', 'wb') as f:
pickle.dump(pc, f)
except:
traceback.print_exc()
Ausgabe auf der Shell:
$ python3 prova.py
Traceback (most recent call last):
File "prova.py", line 16, in <module>
pickle.dump(pt, f)
_pickle.PicklingError: Can't pickle <class 'typing.PersonTyping'>: attribute lookup PersonTyping on typing failed
$
Dies wurde in 'python 3.5.1' behoben. –