[Python-ideas] PEP 428 - object-oriented filesystem paths

Steven D'Aprano steve at pearwood.info
Sun Oct 7 06:33:42 CEST 2012


On 06/10/12 09:54, Andrew McNabb wrote:
> On Sat, Oct 06, 2012 at 08:41:05AM +1000, Steven D'Aprano wrote:
>> On 06/10/12 05:53, Andrew McNabb wrote:
>>
>>> Path concatenation is obviously not a form of division, so it makes
>>> little sense to use the division operator for this purpose.
>>
>> But / is not just a division operator. It is also used for:
>>
>> * alternatives: "tea and/or coffee, breakfast/lunch/dinner"
>> * italic markup: "some apps use /slashes/ for italics"
>> * instead of line breaks when quoting poetry
>> * abbreviations such as n/a b/w c/o and even w/ (not applicable,
>>    between, care of, with)
>> * date separator
>
> This is the difference between C++ style operators, where the only thing
> that matters is what the operator symbol looks like, and Python style
> operators, where an operator symbol is just syntactic sugar.  In Python,
> the "/" is synonymous with `operator.div` and is defined in terms of the
> `__div__` special method.  This distinction is why I hate operator
> overloading in C++ but like it in Python.

I'm afraid that it's a distinction that seems meaningless to me.

int + int and str + str are not the same, even though the operator symbol
looks the same. Likewise int - int and set - set are not the same even
though they use the same operator symbol. Similarly for & and | operators.


For what it is worth, when I am writing pseudocode on paper, just playing
around with ideas, I often use / to join path components:

open(path/name)  # pseudo-code

sort of thing, so I would be much more comfortable writing either of these:

path/"name.txt"
path+"name.txt"

than

path["name.txt"]

which looks like it ought to be a lookup, not a constructor.



-- 
Steven



More information about the Python-ideas mailing list