First page Back Continue Last page Overview Graphics

Pickling Class Objects

pickle:

import pickle

class A(object):

__counter = 0

def __init__(self):

self.pub = "1"

self.__priv = "2"

self._prot = "3 :-)"

def m(self):

print("Just a method")

a = A()

fh = open("test.pkl", "bw")

pickle.dump(a,fh)

unPickle:

import pickle

class A(object):

__counter = 0

def __init__(self):

self.pub = "1"

self.__priv = "2"

self._prot = "3 :-)"

def m(self):

print("Just a method")

fh = open("test.pkl", "br")

a = pickle.load(fh)

print(a)