[path-PEP] Path inherits from basestring again

phil hunt zen19725 at zen.co.uk
Sun Jul 31 00:47:05 EDT 2005


On Sat, 30 Jul 2005 19:01:49 +0200, Reinhold Birkenfeld <reinhold-birkenfeld-nospam at wolke7.net> wrote:
>phil hunt wrote:
>
>> def normalizePath(p, *pathParts):
>>    """ Normalize a file path, by expanding the user name and getting 
>>    the absolute path..
>>    @param p [string] = a path to a file or directory
>>    @param pathParts [list of string] = optional path parts
>>    @return [string] = the same path, normalized
>>    """
>>    p1 = os.path.abspath(os.path.expanduser(p))
>>    if len(pathParts)>0:
>>       allPathParts = [ p1 ]
>>       allPathParts.extend(pathParts)
>>       p1 = os.path.join(*allPathParts)
>>    p2 = os.path.abspath(p1)   
>>    return p2
>> normalisePath=normalizePath # alternate spelling 
>> join=normalizePath # it works like os.path.join, but better  
>> 
>> 
>> To be honest I don't see the point of having a Path class. That's 
>> the way Java does it, and I find path handling in Java to be a lot 
>> more of a hassle than in Python. (Actually, most things are more of 
>> a hassle in Java, but that's another story).
>
>You see, with the Path class the above function could be written as
>
>def normalizePath(p, *pathParts):
>    """ Normalize a file path, by expanding the user name and getting
>    the absolute path..
>    @param p [Path] = a path to a file or directory
>    @param pathParts [list of string/Path] = optional path parts
>    @return [Path] = the same path, normalized
>    """
>    tp = p.expanduser().abspath()
>    return tp.joinwith(*pathParts).abspath()
>
>That's clearly an improvement, isn't it?

An improvement to what? To how the class is implemented, or to how 
it is used?

If you mean the former, yes is it, due to the os.path module not 
providing a function that does this. 

If you mean the latter, I disagree, because I would then have to 
call it with something like:

   pn = normalizePath(Path(p), q)

and then I would have the problem that (pn) isn't a string so 
calling a function to write some data into the file at that filename 
would no longer work, i.e. this:

   writeFile(pn, someData)

would become this:

   writeFile(pn.getString(), someData)

I don't see what having a Path type buys me. 

-- 
Email: zen19725 at zen dot co dot uk





More information about the Python-list mailing list