Trey Hunner ๐Ÿ

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

โ€œEven if you don't use them directly, dataclasses are a good measuring stick for your class-based code.โ€

Read more ๐Ÿ‘‰ https://pym.dev/friendly-classes/

#Python

Making friendly classes

A friendly class accepts sensible arguments, has a nice string representation, and supports equality checks.

But why are they named this way?

Wouldn't it be simpler if re.fullmatch() was called re.match() or re.โ€search() was called re.match() and the current behavior of re.match() didn't exist at all?

I think so. But it's too late for that.

This is all confusing due an accident of history.

re.fullmatch() didn't used to exist but re.match() did. By the time they invented re.fullmatch(), the name "match" was already taken.

Personally, I usually just stick with re.โ€search().

๐Ÿงต (3/3)

To find a match WITHIN a string, you can use re.โ€search().

To match an entire string against a regex, you can use re.fullmatch().

Using re.fullmatch() is the same as using re.โ€search() with ^ and $ used to anchor to the beginning and end of the string.

In other words, these are equivalent:

match = re.โ€search(r"^\w+$", string)
match = re.fullmatch(r"\w+", string)

re.match() is a weird middle ground between re.โ€search() and re.fullmatch().

๐Ÿงต (2/3)

Python Tip #95 (of 365):

Don't use re.match(): it's confusing.

I'm not sure I've ever seen re.match() used when it wasn't being used by mistake.

If you think you want re.match(), you probably want either re.โ€search() or re.fullmatch() instead.

When matching a regex against a string, we're usually either trying to:

1. Find a regular expression within a string
2. Match a regular expression against a whole string

re.match() does neither of those!

๐Ÿงต (1/3)

#Python #DailyPythonTip

I love the fact that we're at the "Where are we gonna have the game nights?" part of the #PyConUS planning!

(This is not something handled by the organizers of @pycon but by some of the attendees. With that, the conference chair is contributing to the conversation and will, hopefully, be playing with us.)

RE: https://mastodon.social/@coredispatch/116350032773821393

Okay friends, new side quest! I've started a Python core development newsletter!

If you've ever wanted a regular summary straight to your inbox about all the cool things happening in CPython (and adjacent areas), this is it! Like and subscribe!

First edition, room to grow! ๐ŸŒณ

I'm pretty sure Mastodon lists are private and none of you can tell you're on my list, but there's now 45 of you on there.

If you're coming and want to say hey, please do favorites/reply!

Looking forward to seeing you at PyCon.

Here's a #PythonOddity for you. You can use `float('nan')` as a key in a dictionary. But if you then try to access `my_dict[float('nan')]` you'll get a `KeyError`. If you assign `float('nan')` to a variable and then use that variable to set/access the dictionary that will work.
This happens because `float('nan') != float('nan')`. Even though the hashes are the same, the values are unequal and the dictionary lookup includes an equality check.

I'm getting an early start on my personal "list of folks in the fediverse who will be at @pycon this year".

If you'll be at #PyConUS and would like to run into each other, ๐Ÿ‘‹ reply (or favorite this post or something else that indicates "yes").

โ€œPython's if statements don't use parentheses.โ€

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.