[Biopython-dev] Using .format() method of strings in Biopython?

Peter Cock p.j.a.cock at googlemail.com
Thu Nov 5 10:22:42 UTC 2015


On Thu, Nov 5, 2015 at 3:40 AM, Tiago Antao <tra at popgen.net> wrote:
> On Tue, 13 Oct 2015 12:03:03 +0100
> Peter Cock <p.j.a.cock at googlemail.com> wrote:
>
>> As part of https://github.com/biopython/biopython/pull/635
>> Carlos is suggesting replacing some of the % string formatting
>> (which is like the C sprintf function) with the Python
>> string .format() method.
>>
>> e.g.
>>
>> "Codon '%s' is invalid" % codon
>>
>> becomes:
>>
>> "Codon '{0}' is invalid".format(codon)
>>
>> https://pyformat.info/
>> https://docs.python.org/2/library/string.html#formatstrings
>> https://docs.python.org/2/library/stdtypes.html#string-formatting
>>
>> Note the later says the following about the .format method:
>>
>> "This method of string formatting is the new standard in Python 3"
>
>
> While I am a certified Python 3 zealot, in this case I do not think
> this is always a great idea.
>
> string % values is fine for simple cases (like the above, for example).
> It is problematic in cases with tuples and dictionaries, but not in
> very simple stuff that we tend to use a lot - and that is fine.
>
> While the format method is the new standard, % is in now way going to
> be deprecated - We can keep it without fear of breaking future
> compatibility or existing Python 3 features.
>
> I would maintain neutrality in this case.

Personally I'm very used to the % style, in part due to learning
C before Python, and find it fine for simple cases with just a few
variables.

We can leave this down to each module owner's preference
for the code itself, rather than explicitly stating a convention.

It would be best to be consistent for the user documentation,
where for now I suggest we stick with simple Python 2 and 3
compatible print function/statements:

print("one string only")
print("Hello %s, this is Python %i" % (name, sys.version_info[0]))

Peter


More information about the Biopython-dev mailing list