4 ms·
IMO, this is a defect in the language: the lack of a "must_use" annotation or similar. If that annotation existed, and the .upper() method was annotated with it
by cesarb 6y ago
IMO, this is a defect in the language: the lack of a "must_use" annotation or similar. If that annotation existed, and the .upper() method was annotated with it, the compiler could warn in that situation.
- diarrhea 6y agoBut you are free to do if title == user_input.upper(): That is, you convert a string to upper without binding the result to a name. You just use it in-place and discard the result, which is fine. With compiler, you mean mypy or linters?
- TkTech 6y agoThat's still "using" the resulting value for a comparison. CPython isn't an optimizing compiler, or it would completely remove the call to upper(). >>> def up(v): ... v.upper() ... >>> dis.dis(up) 2 0 LOAD_FAST 0 (v) 2 LOAD_METHOD 0 (upper) 4 CALL_METHOD 0 6 POP_TOP 8 LOAD_CONST 0 (None) 10 RETURN_VALUE >>> def up(v): ... if v.upper() == "HelloWorld": ... return True ... >>> dis.dis(up) 2 0 LOAD_FAST 0 (v) 2 LOAD_METHOD 0 (upper) 4 CALL_METHOD 0 6 LOAD_CONST 1 ('HelloWorld') 8 COMPARE_OP 2 (==) 10 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (True) 14 RETURN_VALUE >> 16 LOAD_CONST 0 (None) 18 RETURN_VALUE Notice in the first example, right after CALL_METHOD the return value on the stack is just immediately POP'd away. The parent is saying that when you run `python example.py` CPython should see that the return value is never used and emit a warning. This would only happen because `upper()` was manually marked using the suggested `must_use` annotation.
- delaaxe 6y agoHe meant that writing a line of code with only contents: msg.upper() should trigger a warning as this clearly doesn't do anything.
- BiteCode_dev 6y agoPython is interpretted, not compiled, and completly dynamic. You cannot check much statically. In fact, any program can replace anything on the fly, and swap your string for something similar but mutable. It's the trade off you make when choosing it.
- pansa2 6y agoI agree, there’s no way to issue a warning about a bare `s.upper()` at compile time. I wonder if it would be possible at runtime?
- nomel 6y agoWhat is this “compile time” you speak of?
- pansa2 6y agoWhen the Python source code is compiled into bytecode.
- nomel 6y agoThat byte code is then interpreted at runtime, so the meaning of s.upper() could change. What something does, when it’s parsed, is not fixed. You can definitely catch most cases at runtime. I’ve done something like this, in an library, to catch a case where people were treating the copy of data as a mutable view. interface[address][slice] = new_values # fancy noop Where a read, modify, write was required: byte_values = interface[address] byte_values[slice] = new_values interface[address] = byte_values It would log/raise a useful error if the there was no assignment/passing of the return value.
- mark-r 6y agoDon't think so, Python doesn't really care if you dispose of the results of an expression. Think about the problems you'd have with ternaries.
- deleted 6y ago[deleted]