any trick to allow anonymous code blocks in python?

Mark Hahn mark at prothon.org
Sat Jun 26 03:03:08 EDT 2004


Doug Holton wrote:
> Mark Hahn wrote:
>  > Prothon by the way doesn't have anonymous code blocks, but it can
>  do: >
>  >     b = button()
>  >     def b.onClick():
>  >             print "You clicked me"
>
> That's the alternative David Eppstein mentioned, and I like it even
> better than my =: suggestion ( b.onclick =: <code block> ).

FYI...

If you don't need b, you could also do this:

        def button().onClick():
                print "You clicked me"

If you need b and you want to write compact (some would argue Perl-like)
code, you could use a new syntax we are debating right now that allows
assignments-as-expressions with ":=".

        def (b := button()).onClick(): print "You clicked me"

There are valid non-Perl-like uses for := also. :-)





More information about the Python-list mailing list