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

Although be careful with that in Python because there are other situations where (x) and (x,) are both legal but mean very different things.


In Python the comma acts as a tuple creation operator inside parentheses with no other context like function invocation.

Also, last I remember in JS a trailing comma meant the creation of an extra array element that’s null so I became conditioned to only use trailing commas for separators in languages where it’s consistent and specified very explicitly like in HCL


Tuple creation and destructuring does not require parentheses in Python.

     w   =  0,   # w == (0,)
    (x)  = (0,)  # x == (0,)
     y,  =  0,   # y == 0
    (z,) = (0,)  # z == 0
Though in Rust, parentheses are required for tuple creation and destructuring.


Trailing commas in JS don't do that, but having more commas than values does. (eg. [1,,].length === 2)




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

Search: