Trey Hunner 🐍

@treyhunner
2.5K Followers
310 Following
4.3K Posts

#Python & #Django educator & team trainer

I help folks sharpen their Python skills with https://PythonMorsels.com🐍πŸͺ

#pythonoddity

Also: ostrovegan, sentientist, YIMBY, and I think economics is highly underrated. I don't post about any of those topics very often.

he/him

πŸ’Œ My Weekly Newsletterhttps://pym.dev/newsletter
🐍 Python Exerciseshttps://www.pythonmorsels.com
πŸ“Ί YouTubehttps://www.youtube.com/@PythonMorsels
πŸ•Έ Personal Bloghttps://treyhunner.com

β€œIt's also printing out the programmer-readable representation instead of the human-readable one, which makes it a little bit clearer that answer is currently a string, but it's supposed to be a number.”

Read more πŸ‘‰ https://pym.dev/debugging-with-f-strings/

#Python

Debugging with f-strings

If you're debugging Python code with print calls, consider using f-strings with self-documenting expressions to make your debugging a little bit easier.

For example, it may not be clear what this condition checks:

if len(text) - len(text.lstrip()) >= 4:
...

But it's pretty clear what this one checks:

indent_level = len(text) - len(text.lstrip())
if indent_level >= 4:
...

Even if a variable is strictly unnecessary, if it improves readability you should keep it!

🧡 (2/2)

Python Tip #84 (of 365):

Use variables to improve code clarity.

Today's tip is the inverse of yesterday's tip.

Well-named variables CAN make your code more readable and they don't really make your code less efficient (after all, variables are just pointers).

🧡 (1/2)

#Python #DailyPythonTip

β€œAny function or class that accepts an iterable can accept a generator expression.”

Read more πŸ‘‰ https://pym.dev/custom-comprehensions/

#Python

Invent your own comprehensions in Python

Python doesn't have tuple, frozenset, or Counter comprehensions, but you can invent your own by passing a generator expression to any iterable-accepting callable.

Python tip: when you need to show something, repr() goes to nerds, str() goes to normies.

Despite the order of the "for" clauses sounding a bit awkward in English, I do sometimes use comprehensions with nested loops.

Read more πŸ‘‰ https://trey.io/aeo9fg

#Python

Nested list comprehensions

Nested list comprehensions in Python can look complex, but with thoughtful whitespace, they can be pretty readable!

Maybe this is what's most readable:

words = sentence.split()
last_3_words = " ".join(words[-3:])

Or maybe it's this:

last_3_words = " ".join(sentence.split()[-3:])

But if a variable isn't any clearer than the expression it represents, just use the expression.

🧡 (4/4)

You might retort with "but the expression is harder to understand without the variables".

This is clearly subjective and reasonable people may disagree.

If you find one big expression harder to read, then use variables to improve readability.

But there's no reason to embrace variable minimalism OR variable maximalism.

🧡 (3/4)

Intermediary steps CAN have their own variables:

words = sentence.split()
last_3_words_list = words[-3:]
last_3_words = " ".join(last_3_words_list)

But if the variables aren't any clearer than the expression, just use the expression:

last_3_words = " ".join(sentence.split()[-3:])

🧡 (2/4)

Python Tip #83 (of 365):

Don't overuse variables in Python.

Expressions are valid almost anywhere in Python.

If a variable name isn't needed to refer to a value multiple times AND the variable name isn't any clearer than the Python expression that produces its value, just use the expression instead.

🧡 (1/4)

#Python #DailyPythonTip