3 ms·
The very first one I was presented with was the following perversity: from pathlib import Path directory = Path("/etc") filepath = directory / "tes
by Smaug123 9y ago
The very first one I was presented with was the following perversity:
from pathlib import Path
directory = Path("/etc")
filepath = directory / "test_file.txt"
Overloading the addition operation for list concatenation: fine. Something something monads composition symbol. But the division operation?
[Apparently I don't know Windows very well, since I'm told the following sentences are wrong:] Anyway, I thought Python was meant to try and be cross-platform. On Windows, the forward slash is not a path separator.
- deleted 9y ago[deleted]
- Osmose 9y agoThe `joinpath` method on Path objects works just as well. Also AFAIK pathlib.Path handles both forward and backslashes and does the right thing with them (assuming you don't have some very specific needs around, say, not accepting invalid slashes at all).
- Smaug123 9y agoSure, `joinpath` may be the best thing since sliced bread. But I maintain that it's semantically perverse to use the division operator symbol to represent it.
- tokenizerrr 9y agoUsing forward slashes on windows will work fine.
- jwilk 9y agoISTR slash works in most, but not all contexts.
- tomrod 9y agoI'm pretty sure forward slash works fine for file paths in Windows. It's just not the standard if you cut and paste a filepath from Windows Explorer. As a primarily Linux user that was a pleasant surprise because $BigCorpIWorkAt only has a Windows offering. Unless it was Cygwin being sneaky, that is...
- kstrauser 9y agoWhile I get what you mean, we're very used to using "/" to concatenate components into full pathnames: we do it every time we're using the shell. "/" is the canonical pathname concatenation operator, and tricking a specific Python class into also using it feels a lot more natural than the joinpath stuff.
- khedoros1 9y agoWe've got a C++ codebase with its own path class, which works this way. They also used unary operator* (dereference operator) in the string class to return a pointer to the underlying c-string. These are the "clever" kinds of things that the original devs used to get kind of sheepish about when I first brought them up.
- ktRolster 9y agoAt one time, operator overloading was encouraged. See for example, << in the cout command. Everyone was trying to think of new ways to use them.
- khedoros1 9y agoWell, and it worked well enough, in the limited context of our software system. There were a few quirks like that, that everyone knew about. It was kind of funny how opinions changed in the years between when it was written and when I started working there, though.
- stevejohnson 9y agoHaving done a lot of work using both the old os.path library and new pathlib, the ergonomics of pathlib are much, much better. The behavior of the overloaded operator is well-defined, unsurprising, and otherwise intuitive.
- oarfish 9y agoBoost::filesystem also does this with its path class. Needless to say I was amazed to see that the + operator wasn't chosen for path concatenation.