this post was submitted on 26 Jul 2025
73 points (98.7% liked)

Python

7340 readers
61 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
top 13 comments
sorted by: hot top controversial new old
[–] FizzyOrange@programming.dev 27 points 1 week ago (1 children)

To be fair this was quite wtf-free. Mostly just unimportant formatting subtleties and stuff you'd never write.

Python definitely has bigger WTFs, like bool being an int, implicit bool conversion, implicit iteration of strings, etc.

[–] Tja@programming.dev 7 points 1 week ago (1 children)

Bool being an int was part of this.

Sure but that's orthogonal to f-strings.

[–] sugar_in_your_tea@sh.itjust.works 13 points 1 week ago* (last edited 1 week ago)

Hmm: 17/26

Failures were:

  • didn't know about the = option, but once I saw it once, I guessed right a few times
  • never used ^ in Python, and didn't make the connection to XOR (I figured I'd need the math library or something)
  • the r and a with =! (again, never used = option)
  • edge cases in walrus operator and other things unrelated to f-strings

Most of those I missed (and many I got) I'd reject a PR over because it's not obvious enough what's going on. But I think given that, I did reasonably well.

But I learned something. Thanks for the post!

[–] spacemanspiffy@lemmy.world 8 points 1 week ago

This is fun, as was the JS date one. I hope people do more.

Still though, I don't think doing well or not on these necessarily mean you're good or bad with the language.

[–] Eheran@lemmy.world 8 points 1 week ago (2 children)

Walrus operator. What did I just read?

[–] sugar_in_your_tea@sh.itjust.works 12 points 1 week ago (2 children)

I love the walrus operator:

if (x := some_function()):
    do_something(x)
else:
    # x is None or False or something, consider it invalid

The only thing I wish was different is adding a scope, which would make x invalid outside the block. But Python's scoping rules are too dumb to handle this case.

[–] tunetardis@piefed.ca 2 points 6 days ago

I think my most common use case is with dictionary lookups.

if (val := dct.get(key)) is not None:
    # do something with val

I've also found some cases where the walrus is useful in something like a list comprehension. I suppose expanding on the above example, you you make one that looks up several keys in a dict and gives you their corresponding values where available.

vals =  [val for key in (key1, key2, key3) if (val := dct.get(key)) is not None]
[–] joyjoy@lemmy.zip 6 points 1 week ago

Walrus is more useful in a while loop.

while (data := f.read(1024)):
    pass
[–] kryptonianCodeMonkey@lemmy.world 2 points 1 week ago* (last edited 1 week ago)

Walrus operator does an inline assignment to a variable and resolves to the value assigned. If it is in a condition statement, like "if x := y:", it assigns the value of y to x then interprets the expression of the condition as of it just said "if x:". Functionally, that means the assignment happens regardless of the value of y, but the condition only passes if the value of y is "truthy", i.e. if it's not None, an empty collection, numerically equal to zero, or just False.

[–] thingsiplay@beehaw.org 8 points 1 week ago (1 children)

Your final score: 9/26

There were more nuances and surprised than anticipated. But it should be expected, because its on this website. :D But any language with duck typing and lot of magic and interpretation, and a long history of changes and additions, used in a huge variety of environments, is bound to be surprising. I am not surprised that JavaScript and Python are surprising.

What to know about f strings

!r is a thing

!s is a thing

There is some syntax for formatting a float which will completely be forgotten that'll have to be looked up.

There is nothing else worth knowing.

Now lets moan and complain about something actually important. Like repos with languishing PRs, like SQLModel.

[–] salmoura@lemmy.eco.br 3 points 1 week ago

Tap for spoilersI was sure something was wrong with the first question, probably because of the linter in my editor.

TIL about the feature in #4, it'll be handy in my day-to-day businesses.

I forgot about the escaping in #7.

#19: what?

From then on, it was all [uneducated] guesses.

12/26, need more practice.