1. 4
  1. 1

    I don’t think I would use any of these patterns :/

    All of these would be made clearer without the walrus and with a defaultdict and the syntax sugar for get and has. But in the general case where you do have to call a method, I think I’d still do something else.

    For the first few, I think I would have separate calls to has and get.

    I don’t know what I’d do for the case statement, but I don’t like it ;)

    In Julia I rely on constant propagation and just repeat my function calls knowing that they will really only be called once. If my function calls are too verbose I just add a lambda with a short name.

    1. 1

      The point of this post isn’t to promote a pattern. It’s to explain how the walrus operator works.

      Yes. defaultdict is a nicer solution. That’s besides the point. The author needed a way to display how the walrus operator could be used to gather some information and then act on it at the same time. Sure, you could have made the code prettier with a defaultdict, but the dict isn’t the point of the exercise.

      A case statement is antithetical to pythons design, but if you need one then this isn’t too bad of a solution. I’d solve it differently myself, but again, the point of the article is to showcase how the walrus operator works so you can use and abuse it in your code. It’s not a style guide.

      1. 1

        the dict isn’t the point of the exercise.

        I tried to cover that complaint already: “But in the general case where you do have to call a method, I think I’d still do something else.”

        The point of this post isn’t to promote a pattern.

        I disagree. The article makes a bunch of normative claims about how the walrus operator should be used to “streamline” or deduplicate code.

    2. [Comment removed by author]

      1. 2

        Please don’t make low effort posts like this here.

        1. 1

          Sorry, I thought it was funny.