开发者

How save implement class in python?

开发者 https://www.devze.com 2023-01-21 23:40 出处:网络
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

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

精彩评论

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