[Python-ideas] Anonymous blocks (again):

Martin Morrison mm at ensoft.co.uk
Mon May 13 02:30:44 CEST 2013


On 13 May 2013, at 00:40, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Now, do you have any constructive feedback on the PEP that still accounts for Python's lack of a standard location for passing callables to functions, or is this reaction simply a matter of "I don't want to have to type 'f' twice because I don't have to do that in other languages"?
> 
Moving past the outright negative feedback, and having only just seen the PEP, the proposed syntax did strike me as awkward and unintuitive. Maybe there is some explanation for why decorator-like syntax was used - if so, please do link me so I can read up.

What struck me though is that the proposed syntax limits the ability to have multiple "anonymous blocks" within a single statement. Instead, I was thinking some syntax like the following might be nicer:

in x = do_something(in_arg, success_hdlr, error_hdlr):
    def success_hdlr(result):
        ... # Do something with result
    def error_hdlr(error):
        ... # Do something with error

That is instead of a decorator-like syntax, make the "in" keyword reusable to introduce a new block, whose "argument" is a statement that can forward reference some names, which are then defined within the block. This allows multiple temporary names to be defined (each in a separate statement within the block).

Some further thought is required on whether only def (and maybe class) statements should be allowed within the "in" block. Although I guess there's technically nothing wrong with:

in x = y + z:
    y = 12
    z = 30

Other than it's a very verbose way of doing something simple. ;-) But maybe there are more useful examples?

Cheers,
Martin

> Regards,
> Nick.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130513/38794f9b/attachment.html>


More information about the Python-ideas mailing list