4 ms·
One Python gotcha that has bitten people in my company a lot: fun_call('string1', 'string2' 'string3') That is, missing commas a
by pvdebbe 10y ago
One Python gotcha that has bitten people in my company a lot:
fun_call('string1',
'string2'
'string3')
That is, missing commas and subsequent string concatenations can lead to nasty errors. I wish Python didn't nick this from C and would have just enforced the use of + to concat over-length strings, if they need to be split to multiple lines.
- dec0dedab0de 10y agoI love Python, but I have to agree on this one.
- bpicolo 10y agoI mean, this one is solvable by not using magic constants. Pretty easy to avoid. Can also lint it.
- look_lookatme 10y agoHow often do people write production software that doesn't have string literals?
- gakada 10y agoI don't get what you're saying. How is it possible to write code without string literals? subprocess.check_call([ "/usr/bin/env", "echo" "hello world" ])
- nitely 10y agoI think she meant to not pass the strings but assign them to constants and then pass those.
- bpicolo 10y agoYep! But, like I mentioned, linter can catch this too.
- ves 10y agohe probably meant not using string literals / ints outside of top-level declarations, so you would instead assign all these parameters to "constants" at top level and then use those constants in function calls, hence avoiding this error.
- js2 10y agoUnless I'm using a string more than once or its meaning is unclear, I always use the literal. In most cases, I find it more legible than having to go look up a constant's definition. Then yes, I rely on the linter and testing.
- wyldfire 10y agoIn many cases, this invocation of fun_call() would not match its definition signature and it would generate an exception. When that's the case it's not at all a Python gotcha because it halted and forced you to fix the error. I like this string catenation behavior and I prefer it, even if it causes some confusion in (IMO rare) cases.
- xapata 10y agoThey considered dropping that for Python 3. I forget the reason why they changed their minds, but there's probably a PEP about it. You may find that their discussion will change your mind as well.
- pvdebbe 10y agohttps://www.python.org/dev/peps/pep-3126/ https://www.python.org/dev/peps/pep-3126/ Nothing mind-changing in here. Translations seem to take the biggest hit, but it's largely a matter of company conventions if this is a problem. IMO the grounds of rejection weren't discussed very thoroughly.
- xapata 10y agoStatus quo is the sane default. But in this case... I've used this syntax both as a feature and a bug about in equal portion. I could imagine the new f-string interpolation might have some nice synergy.