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

Показать описание
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
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
Комментарии