[Python-Dev] Extended Function syntax

Samuele Pedroni pedronis@bluewin.ch
Mon, 3 Feb 2003 13:05:09 +0100


From: "Guido van Rossum" <guido@python.org>
> > > with myfile = auto_closing_file('blah.txt', 'rb'):
> > >   for line in myfile:
> > >    ...
> > >
> > > With Guido's 'do', you could define an iterclose():
> > >
> > > do iterclose(open('blah.txt','rb')): (line):
> > >    ...
> > >
> > > Btw, the two snippets illustrate quite well the different
> > > evolutive directions' on the table.
> >
> > I must say that, for this particular usage at least, I find
> > the first one a darn sight easier to follow than the second!
>
> I think 'do' is being misrepresented,

OK, to be even more fair, obviously do can be used to implement with

class with:
  def __init__(self,wobj):
    self.wobj = wobj

  def __call__(self,thunk):
     if hasattr(wobj,'__enter__'):
       wobj.__enter__()
     try:
      thunk(wobj)
     finally:
      wobj.__exit__()

do with(auto_closing_file('blah.txt', 'rb')): (myfile): # bad that is not
meaningful: do myfile=with(...):
  for line in myfile:
   ...

FURTHER 'do' is more expressive than generators because it allows to capture
and abstract 'try' patterns very generally, more than 'with'.

>and I also doubt that this is a
> very practical example.

Yes and no. What would be typical is people abstracting over recurrent pattern
of 'for' and 'try' in their program and stuff them in a 'do' behavior, I'm
inferring this from what people do with CL macros. So if one does what
iterclose does a bunch of times in program, she could be tempted to write
iterclose. It's succinctness daemon. General mileage can vary.

> But I may have to wait until the next weekend
> before I can continue in this thread -- I can't work on this on
> workdays.

OK