简体   繁体   中英

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 my class. I don't want to save text of implementation manually.

How can I do this?

使用字典而不是类。

If you don't want to copy your source code to the "other computer" then the best best is to use native data structures, ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM