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

I still don't understand why it doesn't! So I still get bit from time to time.


If a person decides to add parentheses to some booleans or arithmetic,

    (4 + 5) * (8 + 2)

    (this and that) or (theother)
These elements should not become 1-tuples after the interior contents are evaluated. I sometimes add parentheses even around single variables just for visual clarity.

Also, this allows you to do dot-access on int / float literals, if you want to

    # doesn't work
    4.to_bytes(8, 'little')

    # works
    (4).to_bytes(8, 'little')


In principle, a 1-tuple shouldn't even be a thing - any single value is a 1-tuple by itself already. However, in a dynamically typed language, this approach complicates things elsewhere - e.g. if you have a value / 1-tuple that is a list, you'd expect iteration over it to give you list elements, not the single element that is a list. But if you have a value that is a tuple of unknown size, you don't want to special-case iteration for when that size is 1.


It depends what you mean by tuple. In Python, tuples are basically just immutable lists. Just as lists with 1 element are useful, so are tuples with 1 element. You might be dealing with a tuple of unknown length, where the length could be 1. In other contexts, the word "tuple" often carries the connotation of "having a known fixed length", in which case the notion of a 1-tuple as distinct from the value itself is less useful.


Presumably because parantheses don't really have anything to do with tuples, it's commas that do. Parantheses are there to help the parser group things in case of ambiguity, and to support expressions spanning multiple lines.


Since you typed it twice, I don't think it's a typo. It's parentheses not parantheses.


Thank you! I guess spelling from my native language is creeping over to English on occasion :)




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

Search: