"Fifty shades of `sign`"
summary
Compares more than a dozen implementations of a sign(n) function that returns -1, 0, or 1, and demonstrates concrete alternatives: using absolute-value division (abs(x)/x) avoided for float/divide concerns, using boolean-to-int indexing (list indexed by truthy sums) that covers all cases without special-casing zero, and using Python 3.10 match statement for pattern matching. Shows compiled bytecode equivalence between simple if/elif and conditional-expression versions, profiles all implementations, and finds the straightforward if/elif is fastest and simplest in practice.
AI-written summary · about
chapters (25)
- 0:00Introduction
- 1:39About me
- 2:45Twitter
- 3:05Short story
- 4:07Sine function
- 4:50Testing my function
- 5:30Why my code looks like a beginner
- 6:33Conditional expressions
- 8:08Objects as booleans
- 8:47Double conditional expression
- 11:54Absolute value function
- 12:58Division
- 16:54Redundancy
- 18:10Conditional Expression
- 21:39Two Conditions
- 26:21Python
- 27:00Timing
- 27:25Timing table
- 28:08Last function
- 28:49David Wesley
- 29:28References
- 29:44Written version
- 30:05Newsletter
- 30:18Feedback
- 30:45Puppies
related talks
08mTen Ways To Fizz Buzz
27mWhy the float did it NaN?
01h 27mMaking computation easier with cool Numpy tricks
18mmeet the best feature in python 3.10: match-case
42mMatch Case for Beginners
43mThe Zen of Polymorphism: Choosing between isinstance(), methods, and @singledispatch
27mAn Executable Semantics for Faster Development of Optimizing Python Compilers
01h 07mWhat Does It All Really Mean
