Why do this?

Mikael Olofsson mikael at isy.liu.se
Thu Oct 5 06:41:59 EDT 2006



Matthew Warren wrote:
> I learned over the years to do things like the following, and I like
> doing it like this because of readability, something Python seems to
> focus on :-
>
> Print "There are "+number+" ways to skin a "+furryanimal
>
> But nowadays, I see things like this all over the place;
>
> print("There are %s ways to skin a %s" % (number, furryanimal))
>
> Now I understand there can be additional formatting benefits when
> dealing with numbers, decimal places etc.. But to me, for strings, the
> second case is much harder to read than the first.
>   

I agree that the second alternative isn't too readable. It is also not 
too maintainable. My version of the above is most often

    print "There are %(number)s ways to skin a %(furryanimal)s." % vars()

Readable and maintainable.

/MiO



More information about the Python-list mailing list