My first Python program

Seebs usenet-nospam at seebs.net
Thu Oct 14 15:53:15 EDT 2010


On 2010-10-14, Hallvard B Furuseth <h.b.furuseth at usit.uio.no> wrote:
> A class which holds an OS resource like a file, should provide a context
> manager and/or a release function, the latter usually called in a
> 'finally:' block.  When the caller doesn't bother with either, the class
> often might as well depend on the destructor in 'file'.

That makes sense.

In this case, I'm pretty sure context managers aren't the right tool (even
apart from version questions), because they appear to be syntax-level
tools -- but it's a runtime decision how many files I have to open and
close.

> For long strings, another option is triple-quoting as you've seen in doc
> strings: print """foo
> bar""".

I assume that this inserts a newline, though, and in this case I don't
want that.

> I've written such code, but I suppose the proper way is to use a
> classmethod to set it, so you can see in the class how the copyright
> gets there.  SourceFile.<classmethod>() and self.<classmethod>() both
> get called with the class as 1st argument.

Oh, that makes more sense.

>     def setup_copyright(cls, fname):
>         cls.copyright = open(fname).read()
>     setup_copyright = classmethod(setup_copyright)
>     # In python >= 2.4 you can instead say @classmethod above the def.

I *think* I get to assume 2.4.

> SourceFile.__repr__() looks like it should be a __str__().  I haven't
> looked at how you use it though.  But __repr__ is supposed to
> look like a Python expression to create the instance: repr([2]) = '[2]',
> or a generic '<foo instance>': repr(id) = '<built-in function id>'.

Ahh!  I didn't realize that.  I was using repr as the "expand on it enough
that you can see what it is" form -- more for debugging than for
something parsable.

> Python 2.0, found as follows:
> - Google python list comprehensions.
> - Check the PEP (Python Enhancement Proposal) which shows up.  PEPs
>   are the formal documents for info to the community, for the Python
>   development process, etc.  <http://www.python.org/dev/peps/pep-0202/>:
>     Title: 	    List Comprehensions
>     Status: 	    Final
>     Type: 	    Standards Track
>     Python-Version: 2.0

Ah-hah!  That's helpful.  Thanks -- now I know how to find out next time
I'm curious.

-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