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), ), ))
                     )))
```
)

Best regards,
Takuo Matsuoka
_______________________________________________
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/K5RPB4XOQROYTWQHI5A4DVTKB2RNY6OC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to