No macros in Python

Michael Hudson mwh at python.net
Tue Dec 17 11:08:12 EST 2002


"Samuele Pedroni" <pedronis at bluewin.ch> writes:

> I would support such a proposal.

Ah, you did send this to the list :) Here's what I sent in email.

> "Michael Hudson" <mwh at python.net> ha scritto nel messaggio
> news:7h33coweo86.fsf at pc150.maths.bris.ac.uk...
> > with <exp>:
> >   <suite>
[snippery]
> > 'with' would have to become a keyword, so that implies a __future__
> > statement, even though it's kind of hard to see how you would use it
> > as a name.
> 
> maybe we could do with a contextual keyword, and that would also underline
> that this is a 'try' form:
> 
> try with <exp>:
>   <suite>

Maybe.  But that looks as if it should have a finally or an except clause 
-- and not having those is the whole point of the excercise!  But if a new 
keyword is too much, this is an option.

> > I don't think this would be hard to implement.  I'll do a prototype
> > and a proper PEP after 2.3 is out.
> 
> For completeness I think there could be also an option in the design
> solution space using generators (a bit along Beni Cherniavsky's idea ) but
> avoiding the need to support yield inside try finally - I don't think that
> that could ever have a clean semantics -, namely:
> 
> try with <expr>:
>  <suite>
> 
> would be equivalent to:
> 
> _x = <expr>
> _x.next()
> try:
>  <suite>
> finally:
>  _x.next()
> 
> So one could write:
> 
> def locked(lock):
>   lock.acquire()
>   yield None
>   lock.release()
>   yield None
> 
> and use it as such:
> 
> try with locked(lock):
>   ...
> 
> this does not require do introduce and pick new special names and that would
> count as flexibility, has different performance trade-offs :), but the need
> for the second yield and in general is a bit obscure.

Well, if I'm writing the PEP I'll mention it but I think it's disgusting.  
What on earth do generators have to do with anything?

I agree that my proposal might lead to lots of small classes, but I don't 
think this is the answer.

Cheers,
M.

-- 
    FORD:  Just pust the fish in your ear, come on, it's only a
           little one.
  ARTHUR:  Uuuuuuuuggh!
                    -- The Hitch-Hikers Guide to the Galaxy, Episode 1



More information about the Python-list mailing list