4 ms·
A few of the good tidbits in this release: – zoneinfo – dict update operators – str.removeprefix and str.removesuffix – “Any valid expression can now be use
by divbzero 6y ago
A few of the good tidbits in this release:
– zoneinfo
– dict update operators
– str.removeprefix and str.removesuffix
– “Any valid expression can now be used as a decorator. Previously, the grammar was much more restrictive.”
– “Python now gets the absolute path of the script filename specified on the command line (ex: python3 script.py): the __file__ attribute of the __main__ module became an absolute path, rather than a relative path.”
On the last one I rarely if ever use __file__ as a relative path and have definitely typed os.path.abspath(__file__) many many times.
- jxramos 6y agonice, removeprefix/suffix is so pythonic. Great addition I've seen handrolled implementations of that go buggy because they happened to work in some cases but broke others.
- user5994461 6y agoThe same can be done with lstrip("prefix") and rstrip("suffit") for most use cases.
- brilee 6y agoIt doesn't do what you think it does. From https://www.python.org/dev/peps/pep-0616/#rationale https://www.python.org/dev/peps/pep-0616/#rationale "There have been repeated issues on Python-Ideas [2] [3], Python-Dev [4] [5] [6] [7], the Bug Tracker, and StackOverflow [8], related to user confusion about the existing str.lstrip and str.rstrip methods. These users are typically expecting the behavior of removeprefix and removesuffix, but they are surprised that the parameter for lstrip is interpreted as a set of characters, not a substring."
- deleted 6y ago[deleted]
- ATsch 6y agoNo, this is exactly the common coding mistake removeprefix/suffix is here to adress. The strip() family removes all characters in the string you pass, so it removes not just "prefix" but also "efix", "prefixix" and "effxpixpefiii".
- divbzero 6y agostr.lstrip and str.rstrip remove characters not strings, so it leads to 'prix fixe'.lstrip('prefix') truncating 'prix fixe' to ' fixe'.
- jxramos 6y ago"aaamap".rstrip("map") != "aaa"
- rozab 6y agometa, but I upvoted this comment because it illustrates the exact reason why these functions were added. It adds to the conversation in an important way. Is that not the reason for upvoting in general?
- edanm 6y agoThat's kind of complicated, I think people generally use upvotes both to signal that something is wrong, and to signal that the comment detracts from the debate. This case is actually an interesting edge case - I think downvoters are of the mindset that the comment might spread disinformation, therefore it's better to hide it. But I actually agree with you here - this is something a lot of people think, it is wrong, and it's better to explicitly have people see the wrong information, and learn why it's wrong. That's why the PEP itself mentions this as a common misunderstanding!
- codethief 6y ago> – “Python now gets the absolute path of the script filename specified on the command line (ex: python3 script.py): the __file__ attribute of the __main__ module became an absolute path, rather than a relative path.” Good lord, finally! The number of times I've tripped over this.
- IgorPartola 6y agoAnd when it’s coming via a symlink. And on a networked file system that’s been remounted a couple of times. And parts of it are bound to other parts of it. FML.
- codethief 6y agoGood point! Does it follow symlinks before setting __file__? Because in the context of imports it does: https://docs.python.org/3/tutorial/modules.html#the-module-search-path https://docs.python.org/3/tutorial/modules.html#the-module-s... > Note: On file systems which support symlinks, the directory containing the input script is calculated after the symlink is followed. In other words the directory containing the symlink is not added to the module search path.