Malcolm Tredinnick a écrit :

> Method #2: create a dictionary mapping "engine" values to the right
> callable.
> 
>         try:
>             {'google': google,
>              'yahoo': yahoo,
>              # ...
>             }[engine](...)
>         except KeyError:
>             # Engine name is not valid. Handle appropriately here.
>         
> The drawback is the percieved redundancy between the string name and the
> function name. Still, I would prefer this solution. It automatically
> only allows "permitted" value through -- and if, say, you wanted to
> temporariliy disable the yahoo functionality, you would just comment out
> or remove that line from the dictionary. It also means you don't *have*
> to call your functions by the same name as the search engine, which
> would be useful if you ever included an engine whose name was not a
> valid Python identifier, for example.
> 
> By the way, this second pattern is more-or-less idiomatic Python for a
> C-style switch-statement. Usually preferable to a series of if...elif...
> statements, particularly for a large number of choices.

Thanks for the answer. I'll implement the 2nd method, which makes far 
more sens than my first "harsh and straight away" approach ;-)

Nicolas


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to