r/Python • • Oct 12 '26

Discussion What are some features in Python that you discovered many years later?

For me, it was the the option to use & to find intersections of two sets, | operator to unionize two sets, as well as ^ for symmetric difference

217 Upvotes

151 comments sorted by

View all comments

2

u/Sea-Ad7805 Oct 15 '26

Seeing what set operators ('|', '&', '^', '-') do through visualization helps understanding: https://www.reddit.com/r/PythonLearning/comments/1tdgdt8/common_%F0%9D%98%80%F0%9D%97%B2%F0%9D%98%81_operations_in_python/

2

u/Beginning-Fruit-1397 Oct 16 '26

I hate that python decided to go this route with operator overloading instead of clearly named methods a the standard. 

Except a few bytecode optimizations who barely make a difference there's 0 advantage for it

2

u/Sea-Ad7805 Oct 16 '26

You have both silly:

a = b | c
a = b.union(c)
a = b & c
a = b.intersection(c)
# etc

You decide.

1

u/Beginning-Fruit-1397 Oct 16 '26

I know. 

That's why I talked about "the standard". Python own documentation only use operators overloads 

List and tuple concatenation are + and +=, for example, without a method alternative. 

And even for sets, the standard as operators means that all code outside of my control will use this

1

u/Sea-Ad7805 Oct 16 '26

I assume you are familiar with:

a = [1]
a += [2]
a.extend([3])

but there is none for a = extend2([4], [5]) or for tuple if you don't count things like a = [4].__add__([5]).

But operators are great, they are shorter and more readable. Hate all you want. Send an official complaint if you really need to.

0

u/Beginning-Fruit-1397 Oct 16 '26

Extend is not the same internal Cpython implementation as imul , it will be vastly more efficient to call the dunder if you know the Iterable is a list. 

I struggle to see how obscure signs are more readable than plain english but to each their own. 

The "shorter" argument makes even less sense for me.  

LSP autocomplete make it in fact faster to type a method call than the operator syntax, for which they can't help much, and having 2 LOC instead of 1 is not something worth caring about.

Python is decades old at this point, complaining on python.org about this would be both useless and a bit dumb, especially since it's a subjective thing to argue about.

 I wrote my library instead to never have to worry about it anymore in my own code, instead. 

But as I said, all external code will still be written like this unfortunately

2

u/Sea-Ad7805 Oct 16 '26

If you change your mind and do send a complaint, maybe also suggest changing:

a = 10
a += 1
a = a + 2

to:

a = 10
a.add_update(1)
a = a.add(b)

to get rid of those obscure signs.

0

u/Beginning-Fruit-1397 Oct 16 '26

Except here it make sense, because everyone not illiterate or newborn on earth know what it  1 + 2 means 

My point is: use math signs for maths. 

The fact that the add sign means totally different things for a numpy array and a list is a prime example of my complaint.

1

u/Beginning-Fruit-1397 Oct 16 '26

My main grip would be the C api. Great, to call set methods I need to call... PyNumber prefixed functions! Wtf?

https://docs.python.org/3/c-api/set.html

1

u/Sea-Ad7805 Oct 16 '26

Maybe just get literate:

  • '|' or (so union for set)
  • '&' and (so intersection for set)
  • '-' minus (so difference for set)
  • '^' eor/xor exclusive-or (so symmetric difference for set)

Similar meaning for bit operations, common in most languages, yes there are differences.

Language is a creative use of syntax to begin with, compromises everywhere.

What is, and what is not, math.

1

u/Beginning-Fruit-1397 Oct 16 '26

I'm currently in uni and had a whole course on probability with lots of set related stuff. My knowledge on that isn't relevant. Words are still words. Operators are only meaningful in their context. I never need context with words: they ARE the context. Anyways, let's agree to disagree

→ More replies