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

    if a := 1 or b := 0:
        return a + b
Good luck debugging why this doesn’t work.


This statement returns a syntax error. But I get what you mean. For

  if (a := 1) or (b := 1):
    print(locals())
since a is truthy, it gets bound and is in scope, whereas b is not, which makes sense to me.


To understand that you need to understand “if”, “or” and assignment expressions. You think this is hard to understand, and that’s fair, but the hard point here isn’t the assignment expression, it’s the short circuiting or-statement. You’d have the same issues with debugging “if f() or g():...”

So are you arguing that the language should not have short-circuit or-statements?

That just seems like you are sacrificing tons of usability only to slightly improve on the expectations of those who are just starting python, like the first 1-2weeks of trying to learn it.


I am arguing for short circuiting and against assignment expressions returning. You save one line per assignment while also making everything more confusing. Next thing you know we will be debating why a++ is a thing and how it that now returns so you can use it in if statements.


I believe or has higher precedence than := in this case thus the syntax error. Wrapping := in parentheses, like the other commenter suggested, resolves the issue.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

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

Search: