22.04.21 10:21, Stephen J. Turnbull пише:
> @serhiy Moving my speculative discussion to python-ideas.  CC as
> courtesy, comment from you is welcome but not necessarily expected.
> 
> Serhiy Storchaka writes:
> 
>  > format() without format specifier and str() should return the same
>  > value in general, otherwise it will confuse users.
> 
> I think this is a good rule of thumb because it's a very plausible
> default for format(), and easy to explain.
> 
> But then what is the purpose of format()?  Why not just give str() the
> functionality of format(), and deprecate format() for future use?  Or
> vice versa, format() might be the better name, but the backward
> compatibility implications given the pervasive use of str() would be
> awesome, and not in a good way.

Technically, because the second argument of str() means encoding. So you
would need either make format specifier a third positional parameter,
str(price, None, None, '8.2f') or make it a keyword-only parameter,
str(price, format_spec='8.l2'). Both are look ugly. And since a format
specifier is mutually incompatible with encoding and errors, it is clear
that there should be two distinct functions.

Also, all existing __str__ methods do not accept additional arguments.
There is no way to pass format specifier to it. So we need a new special
method. We could name it __str_ex__, but __format__ seems a better name.

> Given how pervasive both str and format are, and the fact that str
> also has both .format() and .__format__ methods, I guess I'm asking
> for a lot of trouble.  But this plethora of approaches to providing a
> string presentation of an object seems designed to confuse users, and
> it's not clear to me that trying to maintain str(thing) == format(thing)
> necessary helps dissolve that confusion.

It is not hard to maintain because object.__format__ just calls __str__.
If you do not implement __format__, you get it by default. If you
implement __format__ you are free to do what you want.

And usually, if you implement addition and multiplication, you have to
maintain thing+thing == thing*2, but it does not mean that we need to
merge addition and multiplication into single operation.

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FIJCVRUCFADXE22HJZTK2RBPJLELPIKJ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to