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

Nikolaus Rath Nikolaus at rath.org
Mon Apr 11 10:43:49 EDT 2016

On Apr 10 2016, Ethan Furman <ethan-gcWI5d7PMXnvaiG9KC9N7Q at public.gmane.org> wrote:
> On 04/10/2016 02:24 PM, Nikolaus Rath wrote:
>> On Apr 09 2016, Nick Coghlan 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)
> pathlib is the primary motivator, but there's no reason to shut
> everyone else out.
> By having a well-defined protocol and helper function we not only make
> our own lives easier but we also make the lives of third-party
> libraries and experimenters easier.

To me this sounds like catering to a hypothetical audience that may want
to do hypothetical things. If you start with the above, and people
complain that their favorite non-pathlib path library is not supported
by the stdlib, you can still add a protocol. But if you add a protocol
right away, you're stuck with the complexity for a very long time even
if almost no one actually uses it.


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