[Tutor] Storing dictionary value, indexed by key, into a variable
Danny Yoo
dyoo at hashcollision.org
Thu Apr 3 22:52:04 CEST 2014
>> The thing is, this looks really messy Could anyone give me some pointers
>> on how this could be more elegantly done?
>
> Instead of the many if...elif switches try to put the alternatives into a
> list, e. g.
>
>>>> cases = [
> ... "Nominative",
> ... "Genitive",
> ... "Dative",
> ... "Accusative",
> ... "Ablative"
> ... ]
To restate what Peter is presenting: transforming the control flow
structure of the program (if statements) is the key. That is, we take
that control flow structure and rephrase it as _data_ structures
(lists, dicts).
You'll also hear the term "Data Driven Programming" or "Table Driven
Programming" to refer to this idea. For example:
http://blogs.msdn.com/b/ericlippert/archive/2004/02/24/79292.aspx
It's a core idea, and definitely worth practicing by fixing the
program. You'll see all that control flow structure dissolve away.
More information about the Tutor
mailing list