syntax for code blocks
Chris Angelico
rosuav at gmail.com
Tue May 1 10:33:22 EDT 2012
On Wed, May 2, 2012 at 12:18 AM, Kiuhnm
<kiuhnm03.4t.yahoo.it at mail.python.org> wrote:
> "Most Pythonic" doesn't mean better, unfortunately.
>
> For instance, assume that you want to write a function that accepts a
> dictionary of callbacks:
> func(some_args, callbacks)
>
> Pythonic way
> ------------
>
> def when_odd(n):
> pass
>
> def when_prime(n):
> pass
>
> def before_check():
> pass
>
> def after_check():
> pass
>
> func(some_args, {'when_odd' : when_odd,
> 'when_prime' : when_prime,
> 'before_check' : before_check,
> 'after_check' : after_check})
My way:
func(some_args, {'when_odd': lambda: 1,
'when_prime': lambda: 2,
'before_check': lambda: 3,
'after_check': lambda: 4})
In a language where lambda isn't restricted to a single expression (eg
Javascript, Pike), this works even better, but it's still plausible
for many situations.
ChrisA
More information about the Python-list
mailing list