5 ms·
> "foobar".removeprefix("foo") I can't say how much of a nice improve removeprefix() and removesuffix() is. lstrip and rstrip prefix and suffix trimming bugs c
by ed25519FUUU 6y ago
> "foobar".removeprefix("foo")
I can't say how much of a nice improve removeprefix() and removesuffix() is. lstrip and rstrip prefix and suffix trimming bugs crop up in people's code constantly. This seems like a nice change, especially since the alternatives are so ugly.
- ju-st 6y agoThey got rid of the super ugly [:] but introduced a new ugliness.. | and |= operators for dictionaries. Why is this so important that this needs special operators? Python should be readable by everyone who can understand English. I hate when languages keep inventing hieroglyphs, twisting my fingers to type |= takes even longer than typing .join.
- renewiltord 6y agoYou can still do `a.update(b)`
- duckerude 6y agoThe existing alternative to `a | b` was {**a, **b} You could see this as an improvement.
- edgyquant 6y agoAs someone who started out in C I love the pointer style syntax
- rmtech 6y agoOh my god yes.
- philsnow 6y agoas pointed out elsewhere in this thread, `"a" or "b"` evaluates to "a", which kind of makes me think that `a | b` (with a and b as dicts) would take values for overlapping keys from a, not from b. The dict literal with dict-splats conveys a sense of ordering that makes the behavior of taking values for overlapping keys from the lattermost dict make more sense to me.
- meowface 6y agoThis is my favorite change in this release and cause me to upgrade to the RC version a few days ago. It's a huge QoL improvement for so much code I write. I'm also a fan of the walrus operator. These are small, rare, clear, useful, idiomatic new features, in my opinion.
- smitty1e 6y agoIt is outstanding that that the new features have an audience. One frets that Python could become a victim of 'featuritis' as the devs try to shop for a new kitchen sink for each release.
- fireattack 6y agoI personally feel walrus operator is a much more prominent improvement, considering how prevalent it can be used (like, I use it in almost every single `re.search()`, and at least half of conditional list comprehensions and `while`), while I don't find the old way of updating dict (`{a, b}` etc.) too bad. But of course, both are very welcomed!
- amelius 6y ago> They got rid of the super ugly [:] What do you mean by that? Can't we clear a list by doing: L[:] = [] (useful when L is defined in another scope, or when you want to keep a reference to the same list object) Unfortunately it's not possible to search for "[:]", so pointers appreciated
- ed25519FUUU 6y agoWait, why not just L.clear()?
- amelius 6y agoGood point. But you might still want e.g.: L[1:] = []
- ju-st 6y agoI just meant that you need the "[:]-operator" less often because of the new methods removeprefix, not that the thing is completely removed.
- kevin_thibedeau 6y agoThey already existed for sets and some people want dicts to have analogous behavior.
- irrational 6y agoI feel the same way about -> in JavaScript. You would think after all these years I'd have come around by now. Nope, I'm still pissed off about that decision.
- gecko 6y agoHuh? What's this do in JavaScript? (No sarcasm; I'm mostly in this ecosystem, but I have no idea what this is in reference to.)
- irrational 6y agoThey are called arrow functions (-> looks a bit like an arrow).
- ZephyrBlu 6y agoIt's confusing to write it like -> because the actual operator is =>, which is close but not the same.
- arthurcolle 6y agoisn't it =>
- jakogut 6y agoI find this operator intuitive and idiomatic. It's widely understood and used as a binary OR operator in many languages, and it's easy to understand its functionality when both operands are dictionaries.
- nneonneo 6y agoIt’s also already an established part of the set syntax - it’s been possible to ^, | and & sets for a while now (symmetric difference, union and intersection). While not all of those make sense for dicts, | for union does and is quite intuitive.
- sgdpk 6y agoIt's also consistent with the set notation, which makes it easier to remember. Dict keys already kind of work like a set, this extends the behavior to deal with the values as well.
- saiojd 6y agoIt's not introduced at all, its already there for sets
- doc_gunthrop 6y agoIt doesn't replace the `lstrip` or `rstrip` methods; it replaces the need for an `if` statement using `startswith` and `endswith` methods. if "foobar".startswith("foo"): "foobar"[len("foo"):]
- deleted 6y ago[deleted]
- quietbritishjim 6y agoThe parent comment referred to bugs from use of lstrip and rstrip for this purpose. The problem is that they often appear to work for this job, so they can make it all the way into code. Your own example strings illustrate that: > "foobar".lstrip("foo") "bar" Of course, if the developer making this mistake tested with "foofbar" instead then they would realise their mistake. A more typical example of this type of mistake would be url.lstrip("http:// http://").
- doc_gunthrop 6y agoIf the developer is using str.lstrip or str.rstrip, they're doing it wrong (because they're using the wrong method). > "foobar".lstrip("wooowweeezoweeef") bar
- vultour 6y agoYes, that's what both comments you replied to have said.
- digianarchist 6y agoThat always bites people. `lstrip` and `rstrip` take a set of characters to be removed not a prefix/suffix. >>> "foobarbaz".lstrip("fo") 'barbaz'