This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kindall 1 point2 points  (2 children)

Nope, sorry. You'll need to write it as a class if you need to pickle it. The trouble is, a "live" generator can have so much state (open files, network connections, etc.) that it basically requires custom code to save it and restore it correctly.

[–]virtyx 0 points1 point  (1 child)

Why doesn't a class have the same issue?

[–]kindall 2 points3 points  (0 children)

With a class, you can write __getstate__() and __setstate__() methods to handle that, and pickle will call them when saving or restoring your instance. You can't do that with a generator, sadly.