Trey Hunner

@trey.io

Python & Django team trainer I help folks sharpen their Python skills with https://PythonMorsels.com 🐍🍪 I also blog at https://treyhunner.com & run https://Whereabouts.Earth YIMBY. 95% vegan.

Python Tip #217 (of 365): Think in terms of the minimum viable duck 🧵 In other words: "what's the least this object needs to do to work in this context?" Sometimes the answer is a solid noun: iterable. Sometimes it's a fuzzier duck: an object with a write() method. #Python #DailyPythonTip

Python Tip #216 (of 365): Embrace duck typing in Python. 🧵 In Python, we often care more about the behavior of an object than its type. We say "if it looks like a duck and quacks like a duck, it's a duck". We check for duckness, we don't test DNA, we observe behavior. #Python #DailyPythonTip

Python Tip #215 (of 365): Know what it means to be a "sequence" in Python. 🧵 A sequence is an ordered collection that: 1. Has a length 2. Can be indexed from "0" up to one less than its length 3. Can be looped over Lists, strings, and tuples are all sequences. #Python #DailyPythonTip

Python Tip #214 (of 365): Use truthiness for integers with caution. 🧵 Sometimes it helps readability but sometimes it can hurt it. I find: if hours: ... More readable than: if hours > 0: ... But I find: if number % 2 != 0: ... More readable than: if number % 2: ... #Python #DailyPythonTip

Python Tip #213 (of 365): Replace "or"-chained equality comparisons with a containment check 🧵 Instead of this: is_vowel = c == "a" or c == "e" or c == "i" or c == "o" or c == "u" You can write this: is_vowel = c in ("a", "e", "i", "o", "u") #Python #DailyPythonTip

Python Tip #212 (of 365): Use any() and all() to check a condition across every item in an iterable 🧵 This: any_negative = False for n in numbers: if n < 0: any_negative = True break Can be rewritten as this: any_negative = any(n < 0 for n in numbers) #Python #DailyPythonTip

all_good = True
for item in iterable:
    if not condition(item):
        all_good = False
        break

Can be rewritten as:

all_good = all(
    condition(item)
    for item in iterable
)

Also...

any_good = False
for item in iterable:
    if condition(item):
        any_good = True
        break

Can be rewritten as:

any_good = any(
    condition(item)
    for item in iterable
)

Python Tip #211 (of 365): Consider how your Boolean expressions read in English 🧵 For example, you could write "10 > x" or you could write "x < 10". They check the same thing, but I find the latter one more readable. #Python #DailyPythonTip

Using super()[key] won't work because the super class doesn't implement a __getitem__ method, but looking up the __getitem__ attribute on a super() object does work (assuming a parent class implements that method). Read more 👉 https://trey.io/p0h32w #Python

Finding an item in a sorted list of 10 million things: ✅ Binary search: 23 comparisons ❌ The `in` operator: 2+ million comparisons Python's bisect module does binary search for you. New article, with recipes for closest-match and range lookups: pym.dev/binary-search/

Binary search in Python with bisect

Python's bisect module implements binary search for you. Here's how bisect_left, bisect_right, and insort work, plus recipes for finding the closest match or all values in a range.

pym.dev

New article: Binary search in Python 🔍 Python's bisect module implements binary search for you. When is it actually useful? When your lookups are INEXACT: the closest match, the next value, everything in a range, etc. If you're new to binary search, be sure to try the interactive guessing game 🎯

Binary search in Python with bisect

Python's bisect module implements binary search for you. Here's how bisect_left, bisect_right, and insort work, plus recipes for finding the closest match or all values in a range.

pym.dev

I've recently been updating many of my mini #Python helper apps and I made a page for them: www.pythonmorsels.com/tools/ Note that the strptime & format tools now link to CLI versions (strptime-cli & fguess) and the pastebin & REPL tool recently got a big rework. I'd love to hear your thoughts!

Python Tools

Small free Python tools from Python Morsels: an in-browser Python REPL, a Python-aware pastebin, f-string and strptime format finders, undataclass, and more.

pythonmorsels.com

I've been thinking about the "choose between $50K or a 50% chance at getting $1M" question and the odd dicourse around it. Outside of the emotional component, risk/reward trade offs are ALL AROUND US in the real world. Insurance, portfolios makeup, any sort of loan... ALL of finance!