Suggested coding style

Devin Jeanpierre jeanpierreda at gmail.com
Sun Sep 25 04:55:10 CEST 2011


> Padding numbers with leading zeroes is very common. I'm surprised that
> more languages don't make it a string method.

By making it a string method, instead of a function, we force all
implementations of strings to implement that method. That sort of
sucks, and it's a reason not to include it as a method. It can, after
all, be implemented as a function, and in doing so (with the
appropriate operator overloading) that function could work with
multiple implementations of strings. Instead any implementation of a
string must implement that method. That's a waste.

Of course, one could argue that there are very few string
implementations. On the other hand, there were three in the stdlib in
2.x, and there are other concepts of strings (like ropes) that are
more efficient for different use-cases. The third string in the stdlib,
mmap, didn't even bother implementing the full string interface. It's
just so damned big. (Also some methods didn't apply, like zfill, but
hey!)

At the very least, it's sort of ugly from my perspective to tack on
trivial things like this to be string methods.

But sure, yeah, as a bonus you get to do fewer imports. That's pretty
great too, I guess.

Devin

On Sat, Sep 24, 2011 at 10:36 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> MRAB wrote:
>
>> On 24/09/2011 20:10, Tim Johnson wrote:
>>> * Passiday<passiday at gmail.com>  [110924 09:47]:
>>> <...>
>>>> I have been coding in many other languages, most of the time it was
>>>> Java and C#. I don't like the function mess of PHP (ie, loads and
>>>> loads of functions without any namespaces etc), but I'd like to think
>>>> that Python is different.
>>>    It is ...
>>>
>>>> In my brief coding experience I have stumbled upon Python zfill(width)
>>>> method, and I thought, really, do you have to include such a narrow-
>>>> purpose method in the basic method set? Perhaps there are more such
>>>> methods that are nice when you need them, but then again, you don't
>>>> put all the possible methods in the standard set.
>>>    I think that you have raised an interesting question here. I've
>>>    been coding in python for 9 years and I have never used it.
>>>
>>>> Perhaps there is reason such method is in the basic library, and my
>>>> complaints are unbased?
>>>
>>>    It could be some sort of legacy. I imagine we will hear from some
>>>    more senior pythonists on this matter.
>>>
>> The documentation says "New in version 2.2.2".
>
> Which is about nine years old, so roughly half as old as Python itself.
> It's hardly a new feature.
>
> Just because Passiday and Tim don't use zfill doesn't mean it's not useful
> to some people. Here are examples of it in use, or people asking how to
> format numbers with leading zeroes:
>
> http://www.ruby-forum.com/topic/67378
> http://stackoverflow.com/questions/134934/display-number-with-leading-zeros
> http://stackoverflow.com/questions/733454/best-way-to-format-integer-as-string-with-leading-zeros
> http://msdn.microsoft.com/en-us/library/dd260048.aspx
> http://www.ozgrid.com/forum/showthread.php?t=64193&page=1
>
> http://www.google.com/codesearch#algXCqBNNP0/utils/zip2rep.py&q=lang:%5Epython$%20zfill%20case:yes&ct=rc&cd=7
>
> http://www.google.com/codesearch#Wnd3W7C0RPM/lives_guide.tar.bz2%7C0MIk12cLwTg/desub.py&q=lang:%5Epython$%20zfill%20case:yes
>
> Padding numbers with leading zeroes is very common. I'm surprised that
> more languages don't make it a string method.
>
>
>
> --
> Steven
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list