Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Its also one of the more controversial features and its introduction sparked Guido's stepping down.

Since it was introduced, I have been looking but there has been only a handful of times where I could use it to write clearer code.

I'm curious if anyone actually likes it.

edit: A quick search shows that I do use it regularly in while loops, e.g.:

  while result := my_fun():
    ...


IIRC he stepped down because he thought it's a good feature and people pointlessly moaned about it. Not because he thought it's controversial and has enough.

I used it a small handful of times. But it also seems like quite a source of footguns. Many examples here: https://github.com/satwikkansal/wtfpython rely on the misuse of the walrus operator.


I have written Python since 2003 and used walrus only once. It's unnecessary.


Eh, I write a lot of Python and while it's unnecessary, it can make code easier to read if not overused.

  if (config_fn := base_dir / "config").exists():
      ...
is just a tiny bit shorter than this:

  config_fn = base_dir / "config"
  if config_fn.exists():
      ...
but if you do this enough times (if you are handling a lot of possible errors / missing data, etc.) it can definitely shorten the code.


If you have to wrap it in parens, it might be shorter but it's not really clearer.

In other contexts, parens are a good indication it's time for a new line.

But I can agree something like this does look clean and clear:

  if value := obj.get("key"):
      ...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: