Should you use 'not not x' instead of 'bool(x)' in Python? (NO!)

preview_player
Показать описание
not not x is faster than bool(x), but should you use it?

not not x is faster than bool(x) in Python, even though they compute the same thing. But why? And does this mean you should start using not not x? In this video I'll explain why not not x is faster than bool(x), and also why you still shouldn't use it.

Note: technically bool is not a function, it is a class, and by calling the bool "function" I mean calling the constructor of the class.

Erratum: At 2:30 bool() gives False, not True. This doesn't affect the speed considerations in the video.

SUPPORT ME ⭐
---------------------------------------------------

Top patrons and donors: Laura M, Jameson, John Martin, Vahnekie, Pieter G, Sigmanificient

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------

CHAPTERS
---------------------------------------------------
0:00 Intro
0:48 First test not not vs bool
1:24 Name lookup
3:19 Bytecode comparison
4:53 Second test vs dunder bool and nop
5:29 Third test vs if and if not not
6:12 Bytecode of if vs if not not
6:45 Should you use not not?
7:45 Thanks
Рекомендации по теме
Комментарии
Автор

If the performance difference between bool(x) and not not x matters, quite likely Python is not the best language for your use case.

Chalisque
Автор

"If you care about your python performance - don't use Python"
That and similar phrase are the answer to many pythong performance questions.

ABaumstumpf
Автор

Short answer: no.
You write in python for readability;
If you want speed, don’t-not write in C++

nitsanbh
Автор

Nice video!
I think an introduction to python disassembly (including some explanation on different instructions) would be an interesting video :)

nadavgolden
Автор

2:30 I'm being picky but bool() gets you False, all the other builtin types when created with no argument also return their only falsy default value: int -> 0, float -> 0.0, str -> '', list -> [], dict -> {}, set -> set(), bytes -> b''

Square_
Автор

I love to just randomly look at disassembled python functions, somehow it makes me happy

simsanutiy
Автор

This didn't not not confuse me at times. Great deep dive as always. Loving compiler explorer.

MakeDataUseful
Автор

"Just because the video tells you to keep doing what you were already doing doesn't mean it wasn't interesting, right?"

That's why I'm here, to find out why I'm doing what I'm doing. :)

tobb
Автор

i can conceive someone creating a function like:
def bool_(x): return not not x

:p oh, the irony.

danielalvesldiniz
Автор

x^2 being slower than x*x surprised me. But since I've looked at python disassembly and it makes perfect sense. I had presumed python does basic optimizations like that before then. Now I don't assume they do anything.

xCAFEFD
Автор

You are making me look so good at work. People are seeing me as the resident Pythononista, when I am just regurgitating what you are saying. I have even told them about your videos. I appreciate all of your hard work.

stefanobailey
Автор

honestly, while it's interesting, if you're caring this much about that kind of performance optimisation, you'd probably be better off using a language other than python which doesn't have such high function call overheads, etc.

lsp
Автор

I like these kind of vids tbh. Keep showcasing these unique, weird behaviors in python! Useful (sometimes) stuff.

Darkev
Автор

Going back to school in a few weeks and my programming teacher is gonna _hate_ me.

rijaja
Автор

i shouldn't not not use not not x

edhyjoxenbyl
Автор

James, another excellent video. Great job! I also fully agree with your readability argument to not use "not not x".

robertbrummayer
Автор

"computing the same thing" does not mean "works the same way" in python

the_cheese_cultist
Автор

(Disclaimer: I don't think anyone should use not not, since it's wierd and confusing, but) I see it like this: if a small change in your code can make it faster, that is good. A lot of people say Python is not for speed so if you want things to be fast don't use python. But if a change to your code can make a calculation that takes 10 minutes take 5 minutes instead, then it's still going to save you time and it's still good, which is why looking at and thinking about faster ways to do things in Python is still worth it. That said, this 'not not' example is so clunky that I wouldn't ever want anyone to actually use it (their code would just be hard to read).

Alister
Автор

Wonder what would be the speed of
return True if x else False

Darth_Pro_x
Автор

This video triggered my ptsd. I had a cowork who used to write code like that because of "performance". This made reading Python code like reading some cryptic Perl oneliner.

opedruth