为什么我不能腌制 typing.NamedTuple 而我可以腌制 collections.namedtuple?
Why can't I pickle a typing.NamedTuple while I can pickle a collections.namedtuple?
为什么我不能腌制 typing.NamedTuple
而我可以腌制 collections.namedtuple
?我怎样才能做 pickle a NamedTuple
?
这段代码显示了我到目前为止所做的尝试:
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()
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
$
这是一个错误。我已经在上面开票了:http://bugs.python.org/issue25665
问题是 namedtuple
函数在创建 class 时通过从调用框架的全局变量中查找 __name__
属性来设置其 __module__
属性。在这种情况下,调用者是 typing.NamedTuple
.
result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
因此,在这种情况下最终将其设置为 'typing'
。
>>> type(pt)
<class 'typing.PersonTyping'> # this should be __main__.PersonTyping
>>> type(pc)
<class '__main__.PersonCollections'>
>>> import typing
>>> typing.NamedTuple.__globals__['__name__']
'typing'
修复:
NamedTuple
函数应该自行设置它:
def NamedTuple(typename, fields):
fields = [(n, t) for n, t in fields]
cls = collections.namedtuple(typename, [n for n, t in fields])
cls._field_types = dict(fields)
try:
cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
pass
return cls
现在您还可以:
PersonTyping = NamedTuple('PersonTyping', [('firstname',str),('lastname',str)])
PersonTyping.__module__ = __name__
为什么我不能腌制 typing.NamedTuple
而我可以腌制 collections.namedtuple
?我怎样才能做 pickle a NamedTuple
?
这段代码显示了我到目前为止所做的尝试:
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()
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
$
这是一个错误。我已经在上面开票了:http://bugs.python.org/issue25665
问题是 namedtuple
函数在创建 class 时通过从调用框架的全局变量中查找 __name__
属性来设置其 __module__
属性。在这种情况下,调用者是 typing.NamedTuple
.
result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
因此,在这种情况下最终将其设置为 'typing'
。
>>> type(pt)
<class 'typing.PersonTyping'> # this should be __main__.PersonTyping
>>> type(pc)
<class '__main__.PersonCollections'>
>>> import typing
>>> typing.NamedTuple.__globals__['__name__']
'typing'
修复:
NamedTuple
函数应该自行设置它:
def NamedTuple(typename, fields):
fields = [(n, t) for n, t in fields]
cls = collections.namedtuple(typename, [n for n, t in fields])
cls._field_types = dict(fields)
try:
cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
pass
return cls
现在您还可以:
PersonTyping = NamedTuple('PersonTyping', [('firstname',str),('lastname',str)])
PersonTyping.__module__ = __name__