Why can't generators be pickled?

前端 未结 2 1304

Python\'s pickle (I\'m talking standard Python 2.5/2.6/2.7 here) cannot pickle locks, file objects etc.

It also cannot pickle generators and lambda expressions (or

2条回答
  •  没有蜡笔的小新
    2020-12-01 02:33

    You actually can, depending on the implementation. PyPy and Stackless Python both allow this (to some degree anyway):

    Python 2.7.1 (dcae7aed462b, Aug 17 2011, 09:46:15)
    [PyPy 1.6.0 with GCC 4.0.1] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    And now for something completely different: ``Not your usual analyses.''
    >>>> import pickle
    >>>> gen = (x for x in range(100))
    >>>> next(gen)
    0
    >>>> pickled = pickle.dumps(gen)
    >>>> next(pickle.loads(pickled))
    1
    

    In CPython it's also possible to create an iterator object to simulate a pickable generator.

提交回复
热议问题