Skip to content

Instantly share code, notes, and snippets.

@dylanbstorey
Last active May 31, 2017 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanbstorey/ecbd211078ab4ba7183a29eb245c2ef1 to your computer and use it in GitHub Desktop.
Save dylanbstorey/ecbd211078ab4ba7183a29eb245c2ef1 to your computer and use it in GitHub Desktop.
load and save method example
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