3 ms·
FYI, the google style guide (or maybe the internal only version) suggests to avoid initialize-then-assign in favor of single assignment form: unclear_type_
by joshuamorton 7y ago
FYI, the google style guide (or maybe the internal only version) suggests to avoid initialize-then-assign in favor of single assignment form:
unclear_type_thing = ...
if isinstance(unclear_type_thing, list):
group_keys = unclear_type_thing
else:
group_keys = [unclear_type_thing]
statically avoids this problem. In general, prefer immutable variables where possible. Single-assignment form is nice for a lot of reasons, not the least of which is that it avoids this particular gotcha.
And I should add that the "right" way to do this would be to factor this out to a function:
group_keys = coerce_to_list(...)
is much clearer than either block, and avoids the possibility of the issue.
- CydeWeys 7y agoAll of these things are true, but they require a non-trivial level of experience and discipline to avoid most potential gotchas. Your average Python project on the Web isn't written to this level of quality, and when people are learning programming using Python in school they certainly aren't there yet, and are gonna hit all kinds of problems related to this stuff. But is there a way to force immutable variables in Python? You can easily still end up in the same situation when you typo something (easy to do when plurals are involved), and then end up reassigning something when you meant to create a new variable.
- _coveredInBees 7y agoI don't think that's fair to be honest. If you had simply used Pycharm with default settings you would have easily caught the first bug due to the linting. It's a fair complaint, but this specific bug is easy to catch using any modern Python IDE.
- CydeWeys 7y agoI've never found the "Use this specific IDE" defense particularly valid, considering that many IDEs don't have these features and that in other languages the compiler itself protects you. Needless to say, I was not using Pycharm for this development, nor am I likely to install an entire IDE just for a small change I'm making on a random project. It's a non-trivial burden to configure and learn an entire IDE, vs just using what I already know (which is often just emacs).
- marmaduke 7y ago> a non-trivial burden to configure and learn any new tool chain. It's hard to take complaints like this seriously.
- CydeWeys 7y agoIt's even harder to take "The IDE should make up for deficiencies in the language" seriously. In languages that handle this stuff well, you can edit in Notepad and still not make these mistakes. Why push it up several levels to a few specific IDEs that most people don't even use?
- marmaduke 7y ago> Why push it up several levels to a few specific IDEs that most people don't even use? Because those IDEs those solve problems, so that you can close tickets on the project at hand, without having to port it to the best language evar.
- joshuamorton 7y ago> But is there a way to force immutable variables in Python? You can easily still end up in the same situation when you typo something (easy to do when plurals are involved), and then end up reassigning something when you meant to create a new variable. Not always. Mypy has experimental support for `Final[T]` [0], and attrs/dataclasses support final/frozen instances, but that's opt in on a per-argument basis. [0]: https://mypy.readthedocs.io/en/latest/final_attrs.html https://mypy.readthedocs.io/en/latest/final_attrs.html