[Python-ideas] Dunder method to make object str-like
mal at egenix.com
Thu Apr 7 11:26:54 EDT 2016
On 07.04.2016 16:58, MRAB wrote:
> On 2016-04-07 15:10, M.-A. Lemburg wrote:
>> On 07.04.2016 15:04, Chris Angelico wrote:
>>> This is a spin-off from the __fspath__ discussion on python-dev, in
>>> which a few people said that a more general approach would be better.
>>> Proposal: Objects should be allowed to declare that they are
>>> "string-like" by creating a dunder method (analogously to __index__
>>> for integers) which implies a loss-less conversion to str.
>> I must be missing something... we already have a method for this:
> It's for making string-like objects into strings.
> __str__ isn't suitable because, ints, for example, have a __str__
> method, but they aren't string-like.
Depends on what you define as "string-like", I guess :-)
We have abstract base classes for such tests, but there's nothing
which would define "string-like" as ABC. Before trying to define
a test via a special method, I think it's better to define what
exactly you mean by "string-like".
Something along the lines of numbers.Number, but for strings.
To make an object string-like, you then test for the ABC and
then call .__str__() to get the string representation as string.
Professional Python Services directly from the Experts (#1, Apr 07 2016)
>>> Python Projects, Coaching and Consulting ... http://www.egenix.com/
>>> Python Database Interfaces ... http://products.egenix.com/
>>> Plone/Zope Database Interfaces ... http://zope.egenix.com/
::: We implement business ideas - efficiently in both time and costs :::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
More information about the Python-ideas