Created
March 25, 2021 13:00
-
-
Save vaclavcadek/257107867f8130b029663ddb86ea0239 to your computer and use it in GitHub Desktop.
Forward and backward compatibility illustration.
This file contains hidden or 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 json | |
import pickle | |
class Foo: | |
""" An old Foo.""" | |
def __init__(self, a, b): | |
self.a = a | |
self.b = b | |
foo = Foo(1, 2) | |
# store it using pickle | |
state = json.dumps(foo.__dict__) | |
blob = pickle.dumps(state) | |
class Foo: | |
""" A Foo from the future.""" | |
def __init__(self, a, b, c): | |
self.a = a | |
self.b = b | |
self.c = c | |
# re-load into newer class | |
state = json.loads(pickle.loads(blob)) | |
foo = Foo(**state) |
This file contains hidden or 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 json | |
import pickle | |
class Foo: | |
""" A Foo from the future.""" | |
def __init__(self, a, b, c): | |
self.a = a | |
self.b = b | |
self.c = c | |
# store it using pickle | |
foo = Foo(1, 2, 3) | |
state = json.dumps(foo.__dict__) | |
blob = pickle.dumps(state) | |
class Foo: | |
""" An old Foo.""" | |
def __init__(self, a, b): | |
self.a = a | |
self.b = b | |
# re-load into older class | |
state = json.loads(pickle.loads(blob)) | |
foo = Foo(**state) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment