Trey Hunner 🐍

@treyhunner
2.6K 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

Technically yield-from IS slightly different in that it also delegates send, throw, and close calls to the sub-generator. But for most uses of generators, you probably don't care about those.

Using yield-from is usually easier than using a "for" loop that yields every item.

🧡 (3/3)

For example, here's a recursive generator function that yields all files in a directory tree:

def walk(path):
for item in path.iterdir():
if item.‍is_dir():
for x in walk(item):
yield x
else:
yield item

That inner "for" loop ONLY yields. We can replace it with "yield from":

def walk(path):
for item in path.iterdir():
if item.‍is_dir():
yield from walk(item)
else:
yield item

🧡 (2/3)

Python Tip #114 (of 365):

Instead of yielding in a loop, use yield-from.

(Technically the 2 are slightly different, but that fact rarely matters.)

Within a generator function, if you need to "yield" each item from a specific iterable, you could loop over the iterable and "yield" each item.

🧡 (1/3)

#Python #DailyPythonTip

Note that I did try changing the input's autocomplete, role, and type attributes... but that wasn't enough.

I'm considering whether I want to eventually reimplement a form functionality with a ContentEditable instead of an input to remove the iOS form assistant widget as well. That'll require recreating much of the native functionality of an input box, which will be a mess of a change.

RE: https://mastodon.social/@treyhunner/116448540041853304

I've been fighting with iOS Safari's AutoFill bar and keyboard layout for days for https://Whereabouts.Earth before stumbling into fixes.

I ended up:

1. Putting zero-width joiners in between the letters of "country" in the placeholder text for the input box (I'm sickened by this one... 🀒)

2. Pinning the map container to window.visualViewport instead of window.innerHeight because innerHeight was VERY inconsistent

It FINALLY works now... I think?

If you're on iOS let me know.

Python Tip #113 (of 365):

Instead of functools.lru_cache(maxsize=None) use functools.cache.

Both of these decorators will "memoize" a function, caching all arguments passed to it.

lru_cache is great when you want a maximum size to your cache... but if an unlimited cache size is acceptable, just use functools.cache.

#Python #DailyPythonTip

β€œSome folks rely on this as a feature, but others consider it a bug waiting to happen.”

Read more πŸ‘‰ https://pym.dev/implicit-string-concatenation/

#Python

Implicit string concatenation

Python automatically concatenates adjacent string literals thanks to implicit string concatenation. This feature can sometimes lead to bugs.

Any argument that comes after a * in a function definition's arguments MUST be specified as a keyword argument.

So name can be optionally provided but if it isn't provided, it will default to None.

This feature was added in Python 3.5, so many folks got that first idiom engrained in their Python muscle memory without realizing there's a built-in syntax for capturing a specific keyword-only argument.

🧡 (2/2)

Python Tip #112 (of 365):

Don't overlook keyword-only arguments.

Instead of this Python 2-ism:

def __init__(self, *args, **kwargs):
self.‍name = kwargs.pop("name", None)
super().__init__(*args, **kwargs)

You can do this:

def __init__(self, *args, name=None, **kwargs):
self.‍name = name
super().__init__(*args, **kwargs)

🧡 (1/2)

#Python #DailyPythonTip

I made a thing last week and Earth Day seems like a great day to announce it. 🌏

https://Whereabouts.Earth is a fun (for me at least) app for learning the location of every country on Earth.

It uses a very effective learning technique (spaced repetition), but you don't need to know how that part works. It will work.

My geography knowledge has improved a LOT over the past week.

Join me in learning about the only home we'll ever have.

(And please share your feedback if you actually use this)

Whereabouts?

Learn where every country is, a few minutes at a time.

Whereabouts