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

Published: 21 August 2021
on channel: mCoding
117,430
5.3k

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.

― mCoding with James Murphy (https://mcoding.io)

Source code: https://github.com/mCodingLLC/VideosS...
How an x becomes a bool:    • x to bool conversion in Python, C++, ...  

SUPPORT ME ⭐
---------------------------------------------------
Patreon:   / mcoding  
Paypal: https://www.paypal.com/donate/?hosted...
Other donations: https://mcoding.io/donate

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

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Discord:   / discord  
Github: https://github.com/mCodingLLC/
Reddit:   / mcoding  
Facebook:   / james.mcoding  

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