fuel-dev team mailing list archive
-
fuel-dev team
-
Mailing list archive
-
Message #00143
Re: Python discussion: format vs %
I guess I'll let Python documentation speak here by itself:
http://docs.python.org/release/3.1.5/library/stdtypes.html#old-string-formatting-operations
.format() method also allows you to print your custom objects properly
(through __str__ or __repr__, if you defined these magic methods) without
any butthurt.
Also, as an example of proper percent symbol usage:
http://docs.python.org/3/library/logging.html?highlight=logging#formatter-objects
This means, it's correct to write logger.info("I'm a teapot number %d",
teapot_number), but not logger.info("I'm a teapot number %d" %
teapot_number). And that's the only example I know of using % approach in
modern Python code, you don't really need it anywhere else.
On Wed, Dec 11, 2013 at 5:05 PM, Evgeniy L <eli@xxxxxxxxxxxx> wrote:
> Hi,
>
> I several times saw patches with comments that we should use format
> instead of '%' for string formatting.
>
> For example:
>
> In [17]: 'some text {0}'.format('ololo')
> Out[17]: 'some text ololo'
>
> Instead of
>
> In [16]: 'some text %s' % 'ololo'
> Out[16]: 'some text ololo'
>
> Nikolay, as I know you prefer (and most of this comments were from you)
> first method, could you explain for all of us what is the difference? And
> why we should use first method? And maybe we should add this info in some
> of our docs?
>
> Thanks.
>
> --
> Mailing list: https://launchpad.net/~fuel-dev
> Post to : fuel-dev@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~fuel-dev
> More help : https://help.launchpad.net/ListHelp
>
>
--
Best regards,
Nick Markov
References