block/lambda

iu2 israelu at elbit.co.il
Tue Jul 29 06:04:58 EDT 2008


On Jul 29, 9:36 am, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> iu2 <isra... at elbit.co.il> wrote:
> > Is it possible to grant Python another syntactic mark, similar to
> > triple quotes, that will actually make the enclosed code a compiled
> > code, or an anonymous function?
>
> Yes, the syntactic mark you are looking for is 'def'.
>
> Your example becomes:
>
> >>> def dotimes(n, callable):
>
>         for i in range(n): callable(i)
>
> >>> def block(i):
>
>         for j in range(i):
>                 print j,
>         print
>
> >>> dotimes(5, block)
>
> 0
> 0 1
> 0 1 2
> 0 1 2 3
>
> The only thing you asked for that this doesn't do is make 'block'
> anonymous, but actually that is a good thing.
>
> --
> Duncan Boothhttp://kupuguy.blogspot.com

Ok, I've got 2 questions about it:

1. Can you please explain why it is a good thing?
2. Will it be possible in Python 3.0 to do the following:

>>> def dotimes(n, callable):
        for i in range(n): callable()

>>> def block():
        nonlocal i
        for j in range(i):
                print j,
        print

Thanks



More information about the Python-list mailing list