On 2023-04-28 15:25, Matsuoka Takuo wrote:
What if it's done not by format method but by a separete method, say,
format_partially or something?  The method is different from format
also in that it should leave "{{" and "}}" unaltered.

On Fri, 28 Apr 2023 at 00:05, Matthias Görgens
<matthias.goerg...@gmail.com> wrote:

Does it have any use case that's not already served by functools.partial?

I guess you mean given

   partially_formatted_string = unformatted_string.format(color="blue")

that

   partially_formatted_string.format

would be equivalent to

   partial(unformatted_string.format, color="blue")

However, I think a partially formatted string can help since it will
have other methods than format as well.  For instance, given

   unformatted_string = '{color}{text}'

doing the following thing only with functools.partial could require
more care than you would be willing to give.

   string = (unformatted_string.format(color="blue")
             + unformatted_string
             ).format(text="Spanish", color="red")

(One of the easiest ways might be

```
from functools import partial
from operator import call

format_ = unformatted_string.format
string = "".join(map(partial(call, text="Spanish"),
                      (partial(format_, color="blue"),
                       partial(format_, color="red"), )))
```

However, note the following, more straight-forward code doesn't work:

```
string = "".join(map(partial(call, text="Spanish", color="red"),
                      (partial(format_, color="blue"),
                       format_, )))
```
)

Now, how about this?

```
format_ = '{a}{b}{c}'.format

string = ((format_(a=a0) + format_(b=b0)).format(c=c0)
           + (format_(a=a1) + format_(c=c1)).format(b=b1)
           + (format_(b=b2) + format_(c=c2)).format(a=a2)
           ).format(a=a3, b=b3, c=c3)
```

(For instance, the following code fails:

```
from itertools import chain

string = "".join(map(partial(call, a=a3, b=b3, c=c3),
                      chain.from_iterable(
                          map(map,
                              (partial(partial, c=c0),
                               partial(partial, b=b1),
                               partial(partial, a=a2), ),
                              ((partial(format_, a=a0),
                                partial(format_, b=b0), ),
                               (partial(format_, a=a1),
                                partial(format_, c=c1), ),
                               (partial(format_, b=b2),
                                partial(format_, c=c2), ), ))
                      )))
```
)

What happens if you do '{open}...{close}'.partial_format(open='{close}'? You get '{close}...{close}', and you're going to have a problem using that as a format string and replacing only the second '{close}'.

Or how about '{open}...{close}'.partial_format(open='{')? You get '{...{close}'. Try using that as a format string!

That's why I think that the result of a partial format should be an instance of a new class and not a string.

_______________________________________________
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/TQOQ5CRLDTFXO2YS24UMLQB7ANXQNDWI/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to