Re: Pickle Problem



On Mar 15, 10:38 am, Gary Herron <gher...@xxxxxxxxxxxxxxxxxx> wrote:
tonyr1988 wrote:
I'm a complete python n00b writing my first program (or attempting to,
anyway). I'm trying to make the transition from Java, so if you could
help me, it would be greatly appreciated. Here's the code I'm stuck on
(It's very basic):

class DemoClass:
def __init__(self):
self.title = ["Hello", "Goodbye"]

def WriteToFile(self, path = "test.txt"):
fw = file(path, "w")
pickle.dump(self.title, fw)
fw.close()

if __name__=='__main__':
x = DemoClass
x.WriteToFile

It doesn't do any file I/O at all (that I see). I hope my syntax is
alright. If I just call WriteToFile, shouldn't it perform with the
default path? It gives me no errors and pretends to execute just fine.

Several people have pointed out the problem, but when you get that
fixed, I see another bit of trouble. The pickle format is a binary
format (be default), but you don't open the file in binary mode. On
Unix the distinction is (wisely) irrelevant, but on Windows you should
open the file with a mode of "wb" not just "w".

Thanks guys for all the help. Sure enough, it was the parenthesis.
Most of my problems seem to be from under-simplifying (using
semicolons, brackets, etc) or, rarely, over-simplifying (forgetting
parenthesis). The biggest thing that was messing me up was the
mandatory "self" input. For some reason I was thinking that, if I had
parenthesis, I would have to define it. Fixing that works perfectly.

Also, about the binary format for opening files. The program that I'm
working on now is completely Linux-based - it's impossible for it to
work on any other OS. Should I still open with "wb" or not? Either
way, thanks for that tip!

One more (completely irrelevant) question. I don't quite understand
the double-underscore functions / methods / etc, such as __name__,
__main__, __init__. Is there a reason for the double-underscore? Does
it make anything special?

Again, thanks for the help...I'm probably going to ask a lot more of
it before too long. :)

.



Relevant Pages

  • Re: Parsing a chemical formal
    ... > Abigail wrote: ... ;-) Actually it's not an exercise, the perlscript should format Database ... > parenthesis: for instance Fe3. ... You can do this by first capturing ...
    (comp.lang.perl.misc)
  • Re: How do i find and replace words in parenthesis?
    ... it will say Format: Font ... find and replace with special characters, ... letters and numbers enclosed in parenthesis (including the ...
    (microsoft.public.word.docmanagement)
  • Re: reading file to list
    ... data for his programs in the format ... parenthesis in your data file, making it larger for no good reason, ... no good reason, then it would make no sense, I agree. ... The lisp reader by ...
    (comp.lang.lisp)
  • Re: Phone number format
    ... > Access calls it a "mask" but in Filemaker8, is it possible to format ... > a field to make phone numbers appear with parenthesis and hyphen in ... This calculation comes from the custom functions library at ... If there are too few characters, it formats the text in red. ...
    (comp.databases.filemaker)