path module

Ian Bicking ianb at colorstudy.com
Fri Jul 25 19:48:19 CEST 2003


On Mon, 2003-07-21 at 13:16, Jason Orendorff wrote:
> Ian and Holger wondered why 'path' should subclass 'str'.  It's because
> a path is a string.  Benefit: you can pass 'path' objects to functions
> that expect strings (like functions in 'win32file').  I find this
> really useful in practice.

I feel like this would lead to some annoying behavior in some
circumstances.  Most particularly, I'm thinking of:

def dosomething(file):
    if type(file) is type(""):
        file = open(file)
    ...

This isn't uncommon in functions that take pathnames or file objects. 
While isinstance(path, str) works, it was not an option until 2.2.  So
you'd be forced to do str(pathname) sometimes anyway, to deal with this.

Ideally, interfaces would be changed to use a .open() method on the path
instead of opening the string representation (as Holger's implementation
does), so in the long term it would be nice to abandon direct string
representations entirely.  It would also make it more clear when you had
a real path object and when you just had a string.

  Ian







More information about the Python-list mailing list