[Tutor] Saving class to a file!

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed Nov 27 19:33:02 2002


On Wed, 27 Nov 2002, Tiago Duarte Felix wrote:

> i have a class which i need to save to a file... that works just fine...
> i use cPickle to save the class.... but... there is a problem..

Hi Tiago,

Ok, let's take a look.

> when i read the file.. and do
>
> class = cPickle.load(file)
  ^^^^^

This won't work, but for an innoculous reason: the word 'class' is a
reserved word that's special to Python.  It's a keyword, and Python does
not allow us to use it as a variable name:

###
>>> class = 'room'
  File "<stdin>", line 1
    class = 'room'
          ^
SyntaxError: invalid syntax
###

There are other instances of these reserved keywords that Python claims
control over.  We can't name a variable 'for', or 'pass', or 'and', or
'or', for example.

But I'll assume that this is not the real problem that you're running into
with cPickle; you probably named your variable differently.


> i can acces for example class.name but when i call a method in the class
> for example class.start_sim() it doesn't work... and shuts down my
> program...

Without seeing the code, I plead absolute ignorance: I have no clue what
could be going wrong.  *grin*

Since your code sounds like it's pretty short, it probably wouldn't hurt
if you posted it to Tutor.  Can you show us some of the code that you've
written?

Also, I'm a little confused about what you mean about 'shutting down'.
Does it give an error message?  If an error message pops up, please feel
free to paste it with your reply; it may help us see what's going on.


Best of wishes to you!