[Python-ideas] Dunder method to make object str-like

Chris Barker chris.barker at noaa.gov
Thu Apr 7 15:02:16 EDT 2016

On Thu, Apr 7, 2016 at 11:59 AM, Brett Cannon <brett at python.org> wrote:

>   class StringLike(abc.ABC):
>     @abstractmethod
>     def __str__(self):
>         """Return the string representation of something."""
>   StringLike.register(pathlib.PurePath)  # Any 3rd-party library can do
> the same.
> You could also call the class StringablePath or something and get the
> exact same concept across where you are using the registration abilities of
> ABCs to semantically delineate when a class's __str__() returns a usable
> file path.
> The drawback is that this isn't easily backported like `path.__ospath__()
> if hasattr(path, '__ospath__') else path` for libraries that don't
> necessarily have access to pathlib but want to be compatible with accepting
> path objects.

and a plus is that it's compatible with type hinting -- is that the future
of Python???



Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160407/4c8559c6/attachment.html>

More information about the Python-ideas mailing list