[Tutor] Currency conversion

Michael Janssen mi.janssen at gmail.com
Mon Dec 12 19:42:57 CET 2005


On 12/12/05, David Holland <davholla2002 at yahoo.co.uk> wrote:

> wouldn't it be better to change :-
> "def rupees_from_dollars(d_doll):
>     return 43*(d_doll)
>
> etc to" :-
> "def conversiond_doll, x):
>     return x*(d_doll)
> "

> You can then have one function for all currencies and
> would be less code and easier to maintain.

Having just one function instead of several is fine, perhaps even for
the cost of one additional function argument. Good to maintain, nice
flexibility. OTOH usability is important, too. Eg. I'm always driven
crazy by concurrency conversions, never can imaging what to take and
what to get, so I prefer a single pretty clear named function for my
use case like "rupees_from_dollars" instead of a "conversion(d_doll,
dollar_exchange_rate)" (the hard thing for me is to remember, if it
must be dollar_exchange_rate or 1/dollar_exchange_rate)

What you can often/sometimes find in code is an all purpose
function/class with full flexibility and additionally some easy-to-use
functions provided for the common case. As in the email package with
it's message_from_string and message_from_file functions. You can read
about them and their relationship to the package in:
http://docs.python.org/lib/node584.html

regards
Michael


More information about the Tutor mailing list