Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Oct 11 23:43:48 CEST 2009

On Sun, 11 Oct 2009 11:15:06 -0700, TerryP wrote:

> On Oct 11, 3:42 pm, Esmail <ebo... at hotmail.com> wrote:
>> cool .. I hadn't seen that. Not working quite at the 'pythonic' level
>> yet I am not sure I think it's more readable that the if statement.
>> Also, curious if the dictionary approach is more efficient.
> Somehow I doubt that "Look up X in dictionary D" could ever be more
> efficient (in terms of space and time, at least) then "Check if X is
> equal to Y". It's not about what you get in runtime but what you get in
> monkey time.

A single equality check, obviously not.

Now suppose you have 100 conditions to test. Which is faster, one dict 
lookup, or up to 100 separate if X == Y tests? (On average, you need to 
test half the conditions before finding the match.)

Hint: dicts are used internally by Python for storing names.

> I might take flak here, for writing something like 'dict[key]
> (func_args)' instead of something more Pythonic, 

Looking up a first-class function in a dictionary and passing arguments 
to it is perfectly Pythonic.


More information about the Python-list mailing list