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

Nikolaus Rath Nikolaus at rath.org
Sun Apr 10 17:24:00 EDT 2016

On Apr 09 2016, Nick Coghlan <ncoghlan-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org> wrote:
> The equivalent motivating use case here is "allow pathlib objects to
> be used with the open() builtin, os module functions, and os.path
> module functions".

Why isn't this use case perfectly solved by:

def open(obj, *a):
   # If pathlib isn't imported, we can't possibly receive
   # a Path object.
   pathlib = sys.modules.get('pathlib', None):
   if pathlib is not None and isinstance(obj, pathlib.Path):
       obj = str(obj)
   return real_open(obj, *a)

> That leaves the text representation, and the question of defining
> equivalents to "operator.index" and its underlying __index__ protocol.

well, or the above (as far as I can see) :-).


GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F

             »Time flies like an arrow, fruit flies like a Banana.«

More information about the Python-ideas mailing list