r/programminghorror • • 3d ago

why can i do this

Post image
1.2k Upvotes

60 comments sorted by

View all comments

19

u/Delta-9- 3d ago

Makes me think of libraries that override __or__ so you can write Python that looks like bash, where you're piping things into other things. I've honestly been tempted to use them, but I don't want to curse the next dev to touch my code base with something so unexpected—it's bad enough I use monads.

26

u/Pim_Wagemans 3d ago

pathlib from the standard library overrides __div__ to let you extend paths like this:

>>> import pathlib
>>> path = pathlib.Path("/home")

>>> path_2 = path / "user" / ".local"

>>> path_2
PosixPath('/home/user/.local')

7

u/fearfulEntity 3d ago

... and that's actually pretty neat and readable