开发者

How save implement class in python?

I have instance of the my class. I want to save implementation of class with instance's pickle copy in file. After, I want to use this instance to another computer where there is no implementation of the 开发者_JAVA技巧my class. I don't want to save text of implementation manually.

How can I do this?


Use a dict instead of a class.


If you don't want to copy your source code to the "other computer" then the best best is to use native data structures, i.e. dict or list or tuples etc.

Pack what ever you want to pack & store it in these data structures, then simply do this -

import pickle

def save_to_disk(data, filename):
    pickle.dump(data, open(filename, 'w'))
    return

def read_from_disk(filename):
    data = pickle.load(open(filename))
    return data

I have heard cPickle is faster then pickle

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜