[Python-Dev] / as path join operator

Nick Coghlan ncoghlan at gmail.com
Thu Jan 26 02:40:15 CET 2006


Steven Bethard wrote:
> John J Lee wrote:
>> On Thu, 26 Jan 2006, Tony Meyer wrote:
>> [...]
>>> Well, if you include the much larger discussion on python-list,
>>> people (including me) have said that removing __div__ is a good
>>> idea.  If it's included in the PEP, please at least include a
>>> justification and cover the problems with it.  The vast majority of
>>> people (at least at the time) were either +0 or -0, not +1.  +0's are
>>> not justification for including something.
>> <bikeshed>
>>
>> FWLIW, I'm definitely +1 on using / as a path join operator.
> 
> My only fear with the / operator is that we'll end up with the same
> problems we have for using % in string formatting -- the order of
> operations might not be what users expect.  Since join is conceptually
> an addition-like operator, I would expect:
> 
>     Path('home') / 'a' * 5
> 
> to give me:
> 
>     home/aaaaa
> 
> If I understand it right, it would actually give me something like:
> 
>     home/ahome/ahome/ahome/ahome/a
> 
> I don't want to claim this is the most common use case, but I've
> certainly seen auto-generated paths that look like 'a' * 20, and it
> would be a pity if using the / operator for Path objects did the wrong
> thing by default here...

What if we used "subpath" as the name instead of joinpath?

The main appeal to me of the division operation is that it allows multiple 
path elements to be joined on a single line, but the joining method accepts an 
arbitrary number of arguments, which helps with that just as much, and doesn't 
raise precedence and readability questions.

The above example would be:

   Path('home').subpath('a'*5)

An example of retrieving a config file's full name:

Current:   os.path.join(HOME_DIR, APP_DIR, CONFIG_FILE)
Division:  HOME_DIR / APP_DIR / CONFIG_FILE
Subpath:   HOME_DIR.subpath(APP_DIR, CONFIG_FILE)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list