[Python-Dev] Path inherits from string (original) (raw)

BJörn Lindqvist bjourne at gmail.com
Thu Jan 26 17:16:43 CET 2006


This seems to be the only really major issue with the PEP. Everything else is negotiable, IMHO. But the string inheritance seem to be such a critical issue it deserves its own thread. I have tried to address all criticism of it here:

Really, it is the same arguments that have been rehashed over and over again. I also think that they have been suitably countered already - Practicality beats Purity. The fact that you can plug Path into already existing code is a major benefit. It makes it possible to use and integrate the Path class now and not wait until the mythical Python 3000 when every single flaw in Python have been fixed. Remember that the Path module have existed for three years in the wild and is extra-ordinarily well-liked. If the fact that Path inherits from string is such a big deal, then why is the path module so hugely popular?? :)

The inheritance is a design trade-off. An implementation detail, an insignificant wart in an otherwise very practical design. Over the years I have used the path module this wart has never ever hurt me. Not even once. But if someone can construct a Path class that does not inherit from string and that works equally well as Path then ofcourse, that is preferable. However, because of this (and similar issues) I don't think it is possible:

class Foo:
    def __str__(self):
        return "foo"
open(Foo())
TypeError: coercing to Unicode: need string or buffer, instance found

However, I might be wrong because according to [1] it should work. And having to wrap the Path object in str() (open(str(somepath))) each and every time the called function expects a string is not a practical solution.

Also note that because compatibility with existing code is important, Path can not override or change any methods at all from str - Liskov Substitution Principle. As Path is, it only violates LSP once:

>>> type(Path()) == type('')
False

And there is absolutely nothing that can be done about that. As far as I can tell, the string inheritance is either livable with or is a showstopper. If it is the latter, then:

1. Someone has to make the required modifications to the Python
   core.
2. Create a Path class (or adapt the existing one so) that does
   not inherit from string.
3. Release it and wait a few years hoping for it to gain
   widespread acceptance in the Python community.
4. Make a PEP (or adapt this PEP) that gets accepted.

This scenario makes me sad because it basically means that there will never be a Path module in Python, atleast not during my lifetime. :( Why? Because Jason Orendorff's path module already exists and works. But I may be overestimating the problems and doing what Jason suggests in [2] may be enough. Maybe someone who knows what he is talking about can expand on it further?

1 http://mail.python.org/pipermail/python-dev/2001-August/016674.html 2 http://mail.python.org/pipermail/python-dev/2006-January/060030.html

-- mvh Björn



More information about the Python-Dev mailing list