Trouble using pinckle

Pierre-Alain Dorange pdorange at pas-de-pub-merci.mac.com
Wed Jul 2 16:09:07 CEST 2008


Hello,

I'm new to python and i'm deelopping a small game with pygame.
I got lot of fun with python.

Trying to implement a config file to save user score and config.
Reading doc and some tutorial about file handling i read about pickle,
and yes it's very easy to implement.
But i thought i miss something with the roots of python.

I implement a Prefs class to handle config data and add it a load and
save method. It works but when reading self, it OK inside the load
function but outside the pref instance return to it's previus state...
I don't really understand.

Here's the test code :

#!/usr/bin/env python

import os, pickle

kFileName='test.ini'

class Prefs():
    def __init__(self):
        self.test=1
        self.zorglub='bonjour'

    def load(self):
        if os.path.exists(kFileName):
            try:
                print 'test %d (before)' % self.test
                f=open(kFileName,'r')
                self=pickle.load(f)
                f.close()
                print 'test %d (after)' % self.test
            except IOError:
                return 1

        return 0
    
    def save(self):
        f=open(kFileName,'w')
        pickle.dump(self,f)
        f.close()
        return 0

def main():
    p=Prefs()

    p.load()
    print 'test %d (after load)' % p.test
    p.test+=1
    print 'test %d (before save)' % p.test
    p.save()
    print '----------------------'

if __name__ == '__main__': main()


-- 
Pierre-Alain Dorange

Vidéo, DV et QuickTime                  <http://www.garage-video.com/>
Clarus, the DogCow                    <http://clarus.chez.tiscali.fr/>



More information about the Python-list mailing list