c[:]()

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu May 31 04:10:35 EDT 2007


In <mailman.8452.1180592620.32031.python-list at python.org>, Warren Stringer
wrote:

> Oops! guess I should have tested my rather hasty complaint about executable
> containers. This is nice:
> 
> def a(): return 'b'
> def b(): print 'polly! wakey wakey'
> c = {}
> c['a'] = b
> c[a()]()  #works!
> 
> 
> c[a()]() is a switch statement with an amorphous selector- very handy in its
> own right. But, using a() as a generator would be more expressive. This
> seems to work:
> 
> c = [a,a]
> [d() for d in c]

If you are using the list comprehension just for the side effect of
calling `d` then consider this bad style.  You are building a list of
`None` objects just to have a "cool" one liner then.

> But that still isn't as simple or as direct as:
> 
> c[:]()
> 
> Which would you rather explain to a 12-year-old writing code for the first
> time?

for func in funcs:
    func()

Because it is explicit and readable and matches the english description
"for every function in functions: call that function" very closely while a
list comprehension or your "perlish" line noise is much more magic to
explain and harder to remember.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list