A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/2016-October/146765.html below:

[Python-Dev] Default formatting

[Python-Dev] Default formatting [Python-Dev] Default formattingSerhiy Storchaka storchaka at gmail.com
Tue Oct 25 05:37:32 EDT 2016
Classes that doesn't define the __format__ method for custom PEP 3101 
formatting inherits it from parents.

Originally the object.__format__ method was designed as [1]:

     def __format__(self, format_spec):
         return format(str(self), format_spec)

An instance is converted to string and resulting string is formatted 
according to format specifier.

Later this design was reconsidered [2], and now object.__format__ is 
equivalent to:

     def __format__(self, format_spec):
         assert format_spec == ''
         return format(str(self), '')

Non-empty format specifier is rejected.

But why call format() on resulting string? Why not return resulting 
string as is? object.__format__ could be simpler (not just 
implementation, but for understanding):

     def __format__(self, format_spec):
         assert format_spec == ''
         return str(self)

This can change the behaviour in corner case. str(self) can return not 
exact string, but string subclass with overloaded __format__. But I 
think we can ignore such subtle difference.

[1] https://www.python.org/dev/peps/pep-3101/
[2] http://bugs.python.org/issue7994

More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4