[Python-Dev] / as path join operator

Ian Bicking ianb at colorstudy.com
Thu Jan 26 04:02:07 CET 2006


Steven Bethard wrote:
> 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

Both of these examples are rather silly, of course ;)  There's two 
operators currently used commonly with strings (that I assume Path would 
inherit): + and %.  Both actually make sense with paths too.

   filename_template = '%(USER)s.conf'
   p = Path('/conf') / filename_template % os.environ
which means:
   p = (Path('/conf') / filename_template) % os.environ

But probably the opposite is intended.  Still, it will usually be 
harmless.  Which is sometimes worse than usually harmful.

+ seems completely innocuous, though:

   ext = '.jpg'
   name = fields['name']
   image = Path('/images') / name + ext

It doesn't really matter what order it happens in there.  Assuming 
concatenation results in a new Path object, not a str.

-- 
Ian Bicking  |  ianb at colorstudy.com  |  http://blog.ianbicking.org


More information about the Python-Dev mailing list