Skip to content

Instantly share code, notes, and snippets.

@vaclavcadek
Created March 25, 2021 13:00
Show Gist options
  • Save vaclavcadek/257107867f8130b029663ddb86ea0239 to your computer and use it in GitHub Desktop.
Save vaclavcadek/257107867f8130b029663ddb86ea0239 to your computer and use it in GitHub Desktop.
Forward and backward compatibility illustration.
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)
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