Trey Hunner 🐍

@treyhunner
2.5K Followers
325 Following
4.5K 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

We could simplify that code with an assignment expression:

def read_in_chunks(path):
with open(path, mode="rb") as file:
while chunk := file.‍read(8192):
yield chunk

No more duplicated file.‍read(8192) call! πŸŽ‰

Assignment expressions are assignments that can be embedded in another expression.

The := operator that powers them is called "the walrus operator" because it looks like a walrus laying on its side. See the tusks?

More: https://pym.dev/using-walrus-operator/

🧡 (2/2)

Python's walrus operator

Python's "walrus operator" is used for assignment expressions. Assignment expressions are a way of embedding an assignment statement inside another line of code.

Python Tip #107 (of 365):

Use the walrus operator to simplify "while" loops.

This generator function reads a file in chunks:

def read_in_chunks(path):
with open(path, mode="rb") as file:
chunk = file.‍read(8192)
while chunk:
yield chunk
chunk = file.‍read(8192)

Notice that "chunk = file.‍read(8192)" appears twice: once before the loop and once at the end.

This is a somewhat common pattern with "while" loops.

🧡 (1/2)

#Python #DailyPythonTip

For readability's sake, I prefer to use "\N" to include Unicode characters within strings based on their name:

message = "Yay! \N{sparkles}"

The capitalization in the name doesn't matter:

message = "Yay! \N{SPARKLES}"

If you don't know the name of a Unicode character, you could use unicodedata.‍name to look it up:

>>> import unicodedata
>>> unicodedata.‍name("\u2728")
'SPARKLES'

🧡 (3/3)

Unicode characters copy-pasted into Python code can look odd because fixed-width fonts often don't handle non-ASCII characters well.

Using a "\u" or "\U" prefix works:

message = "Yay! \u2728"

But unless you've memorized the fact that "\u2728" represents ✨, you won't know what that string would actually look like.

🧡 (2/3)

Python Tip #106 (of 365):

Name ambiguous unicode characters with "\N{...}"

When you need to represent a non-ASCII character, you can either:

1. Copy-paste the character directly into your file
2. Use a "\u" or "\U" prefix to represent the character by its code point
3. Use "\N" to represent the character by its name

🧡 (1/3)

#Python #DailyPythonTip

β€œParentheses are used for 3 things in Python: calling callables, creating empty tuples, and grouping.”

Read more πŸ‘‰ https://pym.dev/unnecessary-parentheses/

#Python

Unnecessary parentheses in Python

Python's ability to use parentheses for grouping can often confuse new Python users into over-using parentheses in ways that they shouldn't be used.

Trailing commas make reordering items easier and git diffs shorter.

Trailing commas also control auto-formatter styling.

Without a trailing comma, a long function call might become:

print(
"pears", "jujubes", "apples", "bananas", "limes", "blueberries", "lemons"
)

Add a comma after "lemons" and Black/ruff will put each argument on its own line instead.

More on trailing commas: https://pym.dev/trailing-commas-in-python/

🧡 (2/2)

The benefits of trailing commas

Trailing commas make for easier code changes, shorter diffs, and fewer bugs.

Python Tip #105 (of 365):

When breaking a function call or list literal over multiple lines, use trailing commas.

For example, take this list:

fruits = [
"lemons",
"apples",
"watermelon",
]

Moving "lemons" to the end is easy when there's a trailing comma: just cut and paste.

Without trailing commas, moving "lemons" to the end means adding a comma after "watermelon" AND removing one after lemons.

🧡 (1/2)

#Python #DailyPythonTip

RE: https://fosstodon.org/@ThePSF/116403474490412403

Stay at The Westin and play Cabo and other card games and board games in the lobby in the evenings!

All are welcome to join. You'll be able to spot us because we'll look like PyCon attendees. Come over, say hi, play games, and chat.

I'm not making it PyCon US this year because of πŸ’°. But if you are going, please consider staying in the official room block and booking NOW. Here's why: https://pyfound.blogspot.com/2026/04/pycon-us-2026-hotels.html
PyCon US 2026: Why we're asking you to think about your hotel reservation

Python Software Foundation Blog