(Idea) subclassable string: path object?
Every once in a while I wished for an path object to manipulate file system paths. Things like os.path.join(a, b, c, os.path.splitext(os.path.basename(p))[0] + ".ext") quickly get frustrating (so of course I never write them like that ;-). I thought of implementing a path object several times, but always stopped when I realized (for the Nth time ;-) that you'd then have to do something like file = open(p.tostring()) whenever you want to *use* your pat. That doesn't help at all. But: since strings are now subclassable (there are, aren't they?) this should no longer be a problem! Would it be a worthwile project to design and implement a path object for the standard library? Just
Every once in a while I wished for an path object to manipulate file system paths. Things like os.path.join(a, b, c, os.path.splitext(os.path.basename(p))[0] + ".ext") quickly get frustrating (so of course I never write them like that ;-).
I thought of implementing a path object several times, but always stopped when I realized (for the Nth time ;-) that you'd then have to do something like file = open(p.tostring()) whenever you want to *use* your pat. That doesn't help at all.
Two better solutions: (1) If the Path class defines a __str__() method, you can write open(p). (2) You could add an open method to the Path class, and write p.open().
But: since strings are now subclassable (there are, aren't they?) this should no longer be a problem!
Would it be a worthwile project to design and implement a path object for the standard library?
I'm lukewarm on adding this to the std library. Java has such a thing and I never warmed up to it. --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum wrote:
Two better solutions:
(1) If the Path class defines a __str__() method, you can write open(p).
Ah, I had no idea that works these days: excellent, much nicer!
(2) You could add an open method to the Path class, and write p.open().
Mwah. I quite like (1). Maybe I'll just write a Path class and use it for a while, then I can be more sure of it's usefulness (or unusefulness...). I think it'll be a lot like string methods, in the sense that if all you know is the string module string methods seems unneccesary and overkill, but once you start using them you notice how much better it is. I see a Path object as an attempt to OO-ify os.path. I'll report back if it works out well. Just
participants (2)
-
Guido van Rossum
-
Just van Rossum