3 ms·
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 beco
by voussoir 5y ago
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')
- int_19h 5y agoIn 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.
- housecarpenter 5y agoIt 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.