[–] 2 points 1 year 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]
  • source
  • parent
  • context
  • 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.

  • source
  • parent
  • context
  • Python has an "Optional" type, but it's merely an alias for T | None. I wish Python had better support for FP, but each time it gets close, it doesn't quite go far enough.

    For example, it now has match blocks, but there's no error if the match is exhaustive, which hurts formal proofs of correctness. Likewise, tons of other features fall a bit short, but in general it's workable with some discipline.

  • source
  • parent
  • context
  • [–] 2 points 1 year ago (1 child)

    In lots of functional languages, you'd have some_function() return an Option type and then you'd use .map() or similar on it to only do something when the value is defined. I think, that can be done in Python's syntax, but you need a library for the Option type...

  • source
  • parent
  • context
  • [–] 2 points 1 year ago* (last edited 1 year 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.

  • source
  • parent
  • context
  • [–] 4 points 1 year 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.

  • source
  • [–] 13 points 1 year ago (4 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.

  • source
  • parent
  • context
  • 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!

  • source
  • [–] 29 points 1 year ago (2 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.

  • source
  • [–] 8 points 1 year ago (1 child)

    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.

  • source
  • Upvote for the sanity check.

    As the OP mentioned, this is a proposed/draft feature that may or may not ever happen.

    With these kinda posts, should start a betting pool. To put money down on whether this feature sees the light of day within an agreed upon fixed time frame.

  • source
  • parent
  • context
  • [–] 3 points 1 year ago (1 child)

    Does not seems to work on 3.12:

    Python 3.12.11 (main, Jun 29 2025, 16:18:35) [MSC v.1944 64 bit (AMD64)] on win32
    >>> def toto(tata=>[]):
      File "<stdin>", line 1
        def toto(tata=>[]):
                      ^
    SyntaxError: invalid syntax
    
  • source
  • parent
  • context
  • [–] 3 points 1 year ago* (last edited 1 year ago)

    Why the commercial license for pngquant? Maybe rewriting pngcrush IP and slapping a commercial license on it is copyright infringement. This is my impression of Rust. Take others IP, rewrite it in Rust, poof copyright magically transferred. The C99 version how much of that is from prior art?

    Lets just ignore prior art and associated license terms

    pngquant commercial license

    written by Kornel LesiΕ„ski

    ImageOptim Ltd. registered in England and Wales under company number 10288649 whose registered office is at International House, 142 Cromwell Road, London, England, SW7 4EF

    First commit Sep 17th, 2009

    pngcrush license

    Copyright (C) 1998-2002, 2006-2016 Glenn Randers-Pehrson

    glennrp at users.sf.net

    Portions copyright (C) 2005 Greg Roelofs

  • source
  • view more: β€Ή prev next β€Ί