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.lista.append(reg)
    self.nome = "TestStr"
    self.teste = 19900909

    #main
    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__":
    main(sys.argv)
     
    Rogerio Luz, Mar 7, 2011
    #1
    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
    >


    [...snip...]

    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
    #2
    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. roopa
    Replies:
    6
    Views:
    986
    Jerry Coffin
    Aug 27, 2004
  2. a pickle's pickle

    , Aug 2, 2005, in forum: Python
    Replies:
    4
    Views:
    600
  3. dackz
    Replies:
    0
    Views:
    605
    dackz
    Feb 6, 2007
  4. Debajit Adhikary
    Replies:
    17
    Views:
    944
    Debajit Adhikary
    Oct 18, 2007
  5. Michele Simionato
    Replies:
    2
    Views:
    2,201
    Michele Simionato
    May 23, 2008
  6. Peng Yu

    Load a list subset with pickle?

    Peng Yu, Oct 13, 2009, in forum: Python
    Replies:
    0
    Views:
    333
    Peng Yu
    Oct 13, 2009
  7. OW Ghim Siong
    Replies:
    2
    Views:
    630
    Peter Otten
    Nov 30, 2010
  8. asil klin
    Replies:
    28
    Views:
    1,571
    Andreas Leitgeb
    Mar 5, 2011
Loading...