Re: How to do this in Python?



Luis Zarrabeitia <kyrie@xxxxx> writes:

One could use this:

with open(filename, "rb") as f:
for buf in iter(lambda: f.read(1000),''):
do_something(buff)

This is by far the most pythonic solution, it uses the standard 'for'
loop, and clearly marks the sentinel value. lambda may look strange
at first, but this kind of thing is exactly what lambdas are for.
Judging by the other responses, it would seem that few people are
aware of the two-argument 'iter'.

but I don't really like a lambda in there. I guess one could use
functools.partial instead, but it still looks ugly to me. Oh, well,
I guess I also want to see the canonical way of doing it.

I believe you've just written it.
.



Relevant Pages

  • A positive cosmological constant
    ... constant in the Lambda-DMD model or the EFE. ... Based on my own analysis I assumed lambda must have a positive value ... value of lambda when applied to the standard CMD model. ... "particularly pertaining to the nature of dark energy and the ...
    (sci.physics.research)
  • Re: Damage Control! Damage Control!
    ... The statement below contradicts standard cosmology, ... CMBR-anisotropy data and the supernovae red-shift data, ... data gives the same value of Lambda as WMAP. ...
    (sci.physics.research)
  • Re: Understanding # and function variables
    ... > Whether or not this violates some standard doesn't matter much IMHO. ... Lisp (so that, for example, lambda bindings work the right way) then it's ... dynamic binding and a lexical binding for X in the same lambda form) and ...
    (comp.lang.lisp)
  • Re: ML functors vs. C++ templates
    ... Actually, as of Technical Report 1, which has preceded the 0x standard, C++ has curried functors as part of the standard in tr1::bind/tr1::function. ... It allows for some functional idioms to be used in C++. ... The only lambda support in C++ right now is a very complicated library solution, and the lambda proposal hasn't made it into the new standard yet. ...
    (comp.lang.functional)
  • Re: please recommend book or web page on beginners lambda calculus
    ... Even this is not "standard" lambda calculus, ... The problem is that there's no single universal syntax for lambda ... Languages", and the manuscript for Pierce's "Types and Programming ... lambda calculus as nowadays used in programming language semantics. ...
    (comp.lang.functional)