DEV Community

Discussion on: Intersection, union and difference of Sets in Python.

Collapse
 
fluffy profile image
fluffy • Edited

Them being bitwise operators makes them really easy to remember, IMO - after all, what's the fundamental difference between a bitmask and a set of booleans? :)

(Of course in that case difference should look like &~ and not -...)

Collapse
 
rhymes profile image
rhymes

Python is a high level language though, there's no builtin concept of masking bits so, taking into account that we can't assume C knowledge by the reader, I reckon that:

a.intersection(b)

is more readable than

a & b

especially six months from now with a different programmer tasked to fix something 😆

(also remember than there are no other major contexts in Python itself where a & b means anything)

Thread Thread
 
fluffy profile image
fluffy

I agree that the long names are more readable, but Python does provide operators for & and | already; on integers they provide the bitwise logic, same as in C.

>>> 3 & 4
0
>>> 4 & 4
4
>>> 4 & 7
4
>>> 3 | 4
7

Also some things make use of those operators for other purposes; for example, Peewee ORM uses them for its query generator.

Anyway, just because a language is high level doesn't mean it doesn't (and shouldn't) provide bitwise functionality. Bitwise operations are still really useful for a lot of purposes in a lot of fields and I absolutely would not discount how necessary they are.

Thread Thread
 
rhymes profile image
rhymes

Oh thanks fluffy, I totally forgot about those. I rarely see them anywhere that I probably forgot :) My bad!

Anyway, just because a language is high level doesn't mean it doesn't (and shouldn't) provide bitwise functionality. Bitwise operations are still really useful for a lot of purposes in a lot of fields and I absolutely would not discount how necessary they are.

Can't argue with that hehe