My first Python program
Seebs
usenet-nospam at seebs.net
Wed Oct 13 12:56:21 EDT 2010
On 2010-10-12, MRAB <python at mrabarnett.plus.com> wrote:
> The code does require Python 2 and the use of except ... as ... requires
> at least version 2.6.
Whoops.
> Line 51
> The __init__ method should always return None. There's no need to be
> explicit about it, just use a plain "return".
The real issue here is that I was assuming that open('nonexistent') returned
None rather than raising an exception.
> The error message says:
> "Couldn't open %s to read a template."
> but it's opening the file for writing.
Ahh, my famed attention to detail strikes again. :)
> You can't really rely on the destructor __del__ being called.
Interesting. Do I just rely on files getting closed?
> Line 333
> Shouldn't you be checking that the name of the attribute you're setting
> doesn't clash with one of the existing attributes? Are you sure that a
> dict wouldn't be a better idea?
Actually, not sure at all. There's a sort of gradual evolution going on
here, in that I was trying to avoid having a separate dict that I had to
populate with a bunch of stuff -- thus the switch to a hand-written
__getitem__. What I probably ought to do/have done is use a dict first,
and set things in the dict from here, then check the dict first, then
regular getattr, and so on.
> Line 447
>
> The form:
>
> except ... as ...
>
> is in Python versions >= 2.6, but not earlier.
Oops. Is there a way to interact with the caught exception in earlier
Python?
> This use of del just deletes the name from the namespace and won't
> necessarily call the __del__ method of the 'source' object. It's better
> to rely on something more explicit like a 'close' method. (If you can't
> be sure which version of Python it'll use then context managers are
> probably out anyway!)
Okay, good to know. I'll fix that.
-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam at seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
More information about the Python-list
mailing list