[path-PEP] Path inherits from basestring again
Ron Adam
rrr at ronadam.com
Wed Jul 27 19:08:38 EDT 2005
Michael Hoffman wrote:
> Ron Adam wrote:
>
>> In all current cases, (that I know of), of differing types, '+' raises
>> an error.
>
>
> Not quite:
>
> >>> "hello " + u"world"
> u'hello world'
> >>> 4.5 + 5
> 9.5
>
>> Question: Is a path object mutable?
>
>
> No.
>
> This should answer the rest of your questions.
Yes it does, thanks.
In the case of numeric types, it's an addition and not a join. I should
have specified in 'all cases, (I know of), where '+' is used to join
objects, but I thought that was clear from the context of the
discussion. I haven't needed to use unicode yet, so it didn't come to mind.
Although it raises other questions... ;-)
Could a string prefix be used with paths as well?
path = p"C://somedir//somefile"
Would that clash with the 'u' prefix? Or would we need a 'p' and a 'up'
prefix to differentiate the two?
(Just a thought. I'm +0 on this, but this seems to be a new string type,
and file operations are frequent and common.)
You could have both behaviors with the '+'.
path_a + path_b -> join path_b to path_a using separator.
path + string -> append string to path (no separator)
string + path -> prepends string to path (no separator)
This would be similar, (but not exactly like), how u'text'+'text' and
'text'+u'text' work. They both return unicode strings given a standard
string. It would allow the following to work.
path = path('C:')+path('somedir')+path('somefile.txt')+'.zip'
->> 'C://somedir//somefile.txt.zip'
So I guess the question here is which of these is preferable with '+'?
1. Strings act like paths when one is a path. They will be joined with
a separator.
2. Paths are joined with separators *and* a string is prepended or
postpended "as is" with no separator.
3. All path objects (and strings) act like strings. No separator is
used when joining path objects with '+'.
(Seems like #3 defeats the purpose of path a bit to me.)
I'm +1 on #2 after thinking about it.
Cheers,
Ron
More information about the Python-list
mailing list