Code block literals
Greg Ewing (using news.cis.dfn.de)
g2h5dqi002 at sneakemail.com
Sun Oct 12 22:42:20 EDT 2003
Dave Benjamin wrote:
> In that case, why do we eschew code blocks, yet have no problem with the
> implicit invocation of an iterator,
I don't think code blocks per se are regarded as a bad thing.
The problem is that so far nobody has come up with an entirely
satisfactory way of fitting them into the Python syntax as
expressions.
> What if I wrote:
>
> for byte in file('input.dat'):
> do_something_with(byte)
>
> That would be a bit misleading, no?
It certainly would! But I think the implicitness which is
tripping the reader up here lies in the semantics of the
file object when regarded as a sequence, not in the for-loop
construct. There is more than one plausible way that a file
could be iterated over -- it could be a sequence of bytes
or a sequence of lines. An arbitrary choice has been made
that it will (implicitly) be a sequence of lines. If this
example shows anything, it's that this was perhaps a bad
idea, and that it might have been better to make it explicit
by requiring, e.g.
for line in f.iterlines():
...
or
for byte in f.iterbytes():
...
then if you wrote
for byte in f.iterlines():
...
the mistake would stick out just as much as it does in
Ruby.
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
More information about the Python-list
mailing list