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

Random832 random832 at fastmail.com
Thu Apr 7 15:35:57 EDT 2016


On Thu, Apr 7, 2016, at 15:17, Brett Cannon wrote:
> How do you make Python 3.3 code work with this when the ABC will simply
> not
> be available to you unless you're running Python 3.4.3, 3.5.2, or 3.6.0
> (under the assumption that the ABC is put in pathlib and backported
> thanks
> to its provisional status)? The ternary operator one-liner is
> backwards-compatible while the ABC is only forward-compatible.

If it's not available to you, then it's not available to anyone to
register either, so you obviously act as if no objects are StringLike if
you get an ImportError when trying to use it. Isn't this just the
standard dance for using *any* function that's new to a new version of
Python?

try:
    from pathlib import StringLike # if it's in pathlib why is it called
    StringLike?
    def is_StringLike(x): return isinstance(x, StringLike)
except ImportError:
    def is_StringLike(x): return False


More information about the Python-ideas mailing list