At first I was like wtf is that
?
The usage of a tilde
symbol I was able to remember were:
- creating negated (
NOT
) query in django ORM like
- inverting boolean masks in pandas
In python ~
operator means bitwise NOT
. It takes an integer and switch all bits 0
to 1
and 1
to 0
. As wikipedia says `NOT x = -x − 1` (it's different for unsigned int but it's not our case). => ~0 = -0 - 1 = -1
and ~1 = -1 - 1 = -2
So in case of indexing a list the author of items[~index]
wanted to take an element from the right side and use zero-based index from the right side too.
items = ['a', 'b', 'c', 'd', 'e', 'f']
# 0 1 2 3 4 5 # indexes
# -6 -5 -4 -3 -2 -1 # negative indexes
# ~5 ~4 ~3 ~2 ~1 ~0 # tilde indexes
items[0] == items[-6] == 'a' # the first element
items[5] == items[-1] == 'f' # the last element
items[~0] == items[-1] == 'f'
items[~1] == items[-2] == 'e'
It's a very strange way to do indexing. Please don't do that and just use minus indexes. It's much more common and easy to understand.
I have also learned that if you want to support ~
operator for your objects - you can implement magic method __invert__(self)
.