Last active
May 31, 2017 17:49
-
-
Save dylanbstorey/ecbd211078ab4ba7183a29eb245c2ef1 to your computer and use it in GitHub Desktop.
load and save method example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
import os | |
from functools import partial | |
def function(text , a = 3): | |
print('this is a function with text {0} and a = {1}'.format(text , a)) | |
class Test(object): | |
""" """ | |
def __init__(self): | |
""" Constructor for Test""" | |
self.a = 14 | |
self.b = 12 | |
self.c = partial(function , a=3) | |
if os.path.exists("file_name.txt"): | |
self = Test.load() | |
def save(self): | |
with open('file_name.txt','wb') as f: | |
pickle.dump(self,f) | |
@classmethod | |
def load(cls): | |
with open('file_name.txt' ,'rb') as f: | |
obj = pickle.load(f) | |
return obj | |
if __name__ == '__main__': | |
test_1 = Test() | |
print(test_1.a) | |
print(test_1.b) | |
test_1.c("test") | |
test_1.save() | |
test_2 = Test.load() | |
print(test_2.a) | |
print(test_2.b) | |
test_1.c("thing") | |
test_3 = Test() | |
print(test_3.a) | |
print(test_3.b) | |
test_3.c("thing") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment