x to bool conversion in Python, C++, and C

Published: 17 July 2021
on channel: mCoding
38,201
2.2k

How to decide if a variable is truthy.

How do Python, C++, and C decide if "if x" goes down the "true" path or "false" path? Whether you're a Python beginner or an expert Python user, you need to know the rules that are used. Conditionals don't just take conditions. In some languages, like Python and C++, you can put expressions that evaluate to arbitrary objects inside a conditional. In this video we go through the basic rules for when this is allowed in Python, C++, and C, and show the code that determines whether to treat your object like "true" or "false". We use Matt Godbolt's Compiler Explorer to look at the disassembled bytecode or assembly, and then we hunt down the CPython source code for what this bytecode means. This question is exactly the kind of thing that might show up in a Python coding interview, or C++ programming interview to test your knowledge of the language.

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

ceval.c: https://github.com/python/cpython/blo...
object.c: https://github.com/python/cpython/blo...
Compiler Explorer Python example: https://godbolt.org/z/cETPW8Tex
Compiler Explorer C++ example: https://godbolt.org/z/na1hx86Pa
Compiler Explorer C example: https://godbolt.org/z/r9Tj3xY4r

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

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

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

CHAPTERS
---------------------------------------------------
0:00 Intro
0:33 Python bool len rule
1:36 Python compiler explorer
2:01 CPython source
3:00 C++ nums pointers arrays rule
3:37 C++ compiler explorer
4:14 C rule
4:27 Thanks