Code block function syntax, anonymous functions decorator

castironpi at gmail.com castironpi at gmail.com
Wed Feb 6 22:17:57 EST 2008


On Feb 6, 8:10 pm, castiro... at gmail.com wrote:
> On Feb 6, 5:45 pm, Jean-Paul Calderone <exar... at divmod.com> wrote:
>
>
>
>
>
> > On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>
> > >castiro... at gmail.com schrieb:
> > >> def run3( block ):
> > >>    for _ in range( 3 ):
> > >>       block()
>
> > >> run3():
> > >>    normal_suite()
>
> > >> Introduces new syntax; arbitrary functions can follow 'colon'.
>
> > >> Maintains readability, meaning is consistent.
>
> > >> Equivalent to:
>
> > >> def run3( block ):
> > >>    for _ in range( 3 ):
> > >>       block()
>
> > >> @run3
> > >> def anonfunc():
> > >>    normal_suite()
>
> > >> Simplification in cases in which decorators are use often.
>
> > >This is non-sensical - how do you invoke anonfunc? They would all bind
> > >to the same name, run3. Or to no name as all, as your "spec" lacks that.
>
> > As he said, the decorator version is the _equivalent_ to the syntax he
> > was proposing.  The point isn't to decorate the function, so perhaps he
> > shouldn't have used decorator syntax, but instead:
>
> >     def anonfunc():
> >         normal_suite()
> >     run3(anonfunc)
> >     del anonfunc
>
> > So it's not non-sensical.  It's a request for a piece of syntax.
>
> > >Besides, it's butt-ugly IMHO. But taste comes after proper definition...
>
> > It's properly defined.  Not that I'm endorsing this or anything.  I'd
> > rather not see half-assed syntax proposals at all, even if they're super
> > great (and some of the syntax that's made it into Python is much worse
> > than this).
>
> > Jean-Paul- Hide quoted text -
>
> > - Show quoted text -
>
> Yes.  @run3( anonfunc ) runs -in-place-.  Jean-Paul's was a closer
> equivalent.
>
> It's used for a piece of code that won't get called like with
> statements.- Hide quoted text -
>
> - Show quoted text -

I know of two examples.  Thread starting, and event binding in a
framework GUI.  Of course, not too much farther along, for X in A as
C, storing the code block and/or for loop "object" as a object in "C",
and likewise for if.  These met with disfavor in another place.

start_new_thread( codeblock as target, args= () ):
   normal_suite()

bind( "<Key>", codeblock as A ):
   handle_event( event )

But where does 'event' come from?

mybind( "<Key>", codeblock as A )( event ):
   handle_event( event )

?



More information about the Python-list mailing list