4 ms·
tl;dr: Python concatenates space separated strings, so ['foo' 'bar'] becomes ['foobar'], leading to silent bugs due to typos. I've been bitten by this one at w
by routerl 5y ago
tl;dr: Python concatenates space separated strings, so ['foo' 'bar'] becomes ['foobar'], leading to silent bugs due to typos.
I've been bitten by this one at work, and can't help but think it is an insane behaviour, given that ['foo' + 'bar'] explicitly concatenates the strings, and ['foo', 'bar'] is the much more common desired result.
edit: This also applies to un-separated strings, so ['foo''bar'] also becomes ['foobar']
- Palomides 5y agoI assume it's based on the C behavior, where it can be handy with macros I don't think it fits well in python
- pletnes 5y agoI assumed it was borrowed from shell, where everything can just be put next to eachother since it’s all text.
- pmontra 5y agoMaybe. We must remember that Python was designed at the very end of the 80s so what was normal for developers back then could be unexpected nowadays. An example: the self in Python's OO is a C pointer to struct of data and function pointers. It should be perfectly clear to anybody writing OO code in plain C at the time (rising hand.) Five years later new OO languages (Java, Ruby) kept self inside the classes but hide it in method definitions.
- wartijn_ 5y agoBut Python 3 was designed in the 2000s and had many breaking changes. Seems like they could have changed this behavior with that version.
- idealmedtech 5y agoIt's a holdover from C, where implicit string literal concatenation is very useful in the preprocessor.
- thrdbndndn 5y agoI luckily never accidently used this space-concatenation thing, but I've been bitten by the fact a=(1) doesn't create 1-element tuple multiple times in my early days learning Python.
- onphonenow 5y agoI still don't understand why it doesn't! So I still get bit from time to time.
- voussoir 5y agoIf 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.