Pickle a list

Discussion in 'Python' started by Rogerio Luz, Mar 7, 2011.

  1. Rogerio Luz

    Rogerio Luz Guest

    Hi All

    I'd like to pickle an object instance with all values. So I
    instanciate myClass and set some values including a list with more
    values (in the __init__), then dump to file. I realized that the
    pickled object don't saved my new list values (saved only the
    "default" value) but saved a String and an int. What I'm doing wrong?
    Thanks Rogerio

    $ python3 pickler.py P
    Dump: ['default', 1, 2, 3, 4, 5, 6, 7, 8, 9] TestStr 19900909

    $ python3 pickler.py U
    Load: ['default'] TestStr 19900909

    # pickler.py

    import sys
    import pickle

    class MyClass:
    teste = 0
    nome = None
    lista = ["default"]

    def __init__(self):
    for reg in range(1,10):
    self.nome = "TestStr"
    self.teste = 19900909

    def main(argv):
    if argv[1] == "P":
    with open('myClass.pickle', 'wb') as f:
    myClass = MyClass()
    print("Dump:",myClass.lista, myClass.nome, myClass.teste)
    pickle.dump(myClass, f, pickle.HIGHEST_PROTOCOL)

    elif argv[1] == "U":
    with open('myClass.pickle', 'rb') as f:
    myClass = pickle.load(f)
    print("Load:",myClass.lista, myClass.nome, myClass.teste)

    if __name__ == "__main__":
    Rogerio Luz, Mar 7, 2011
    1. Advertisements

  2. On 07-03-11 17:38, Rogerio Luz wrote:
    > import sys
    > import pickle
    > class MyClass:
    > teste = 0
    > nome = None
    > lista = ["default"]
    > def __init__(self):
    > for reg in range(1,10):
    > self.lista.append(reg)

    This probably doesn't do what you think it does.
    It actually appends a range of numbers to the class attribute 'lista',
    and not to the instance attribute of that name (which doesn't exist).

    If you create multiple objects of type MyClass you'll notice that
    everytime the list gets longer and longer (...in *all* objects, because
    they still share the single class attribute!)

    > self.nome = "TestStr"
    > self.teste = 19900909


    The myClass object you're pickling doesn't have a 'lista' attribute.
    While you can print myClass.lista without problems, you're printing the
    class attribute instead.
    Pickle won't include class attributes. It just pickles the object's
    __dict__. If you add a line: print(myClass.__dict__)
    before the pickle() call you'll see that 'lista' is not in there. And
    that means that when you read the pickle back in, the new object won't
    have the 1,2,3,4,5.... numbers in the lista list, instead it just has
    the initial list.

    You probably want to initialize self.alist in the class's __init__
    method instead. That way it is a normal object attribute and will get
    pickled normally.

    Irmen de Jong
    Irmen de Jong, Mar 8, 2011
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Finney

    Securing 'pickle'

    Ben Finney, Jul 11, 2003, in forum: Python
    Paul Rubin
    Jul 11, 2003
  2. Aki Niimura

    freeze utility and pickle

    Aki Niimura, Aug 21, 2003, in forum: Python
    Aug 21, 2003
  3. a pickle's pickle

    , Aug 2, 2005, in forum: Python
  4. Michele Simionato
    Michele Simionato
    May 23, 2008
  5. Peng Yu

    Load a list subset with pickle?

    Peng Yu, Oct 13, 2009, in forum: Python
    Peng Yu
    Oct 13, 2009

Share This Page