News for hackers. Stuff that matters.
Re:The PEPs of Python 3.9 by BiteCode_dev on Thursday May 28, 2020 @03:18PM (#23341227)
If x is near your memory limits, and you do y = x[:-1], you will get a MemoryError :)

For those situations, bytes() + memoryview() or bytearray() can be used, but then you are on your own.

Re:The PEPs of Python 3.9 by ianhorn on Thursday May 28, 2020 @03:36PM (#23341544)
Huh, I've had a wrong understanding of that for over a decade! TIL, thanks.
Re:The PEPs of Python 3.9 by BiteCode_dev on Thursday May 28, 2020 @03:45PM (#23341677)
Hey!

https://xkcd.com/1053/

And honestly, I would be rich if I got a dollar every time a student does this:

    msg.upper()
Instead of:

    msg = msg.upper()
And then call me to say it doesn't work.
Re:The PEPs of Python 3.9 by bjoli on Friday May 29, 2020 @03:01AM (#23347026)
Scheme does the right thing here (by convention), that mutating procedures end with a bang: (string-upcase str) returns a new string, whereas (string-upcase! str) mutates the string in place.

The details for mutation of data in scheme go beyond that, though. Sometimes procedures are "allowed but not required to mutate their argument". Most (all?) implementations do mutate, but it is still considered bad for to do something like:

    (define a (list 1 2 3))
    (append! a (list 4))
    (display a)
As append! returns a list that is supposed to supercede the binding to a. Using a like that "is an error", as a valid implementation of append! may look like this

    (define append! append)
Which would make the earlier code snippet invalid.
Re:The PEPs of Python 3.9 by cesarb on Thursday May 28, 2020 @04:00PM (#23341868)
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.
Re:The PEPs of Python 3.9 by diarrhea on Thursday May 28, 2020 @04:23PM (#23342117)
But 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?

Re:The PEPs of Python 3.9 by TkTech on Thursday May 28, 2020 @04:41PM (#23342276)
That'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.
Re:The PEPs of Python 3.9 by delaaxe on Thursday May 28, 2020 @04:41PM (#23342279)
He meant that writing a line of code with only contents:

    msg.upper()
should trigger a warning as this clearly doesn't do anything.
Re:The PEPs of Python 3.9 by BiteCode_dev on Thursday May 28, 2020 @04:28PM (#23342159)
Python 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.

Re:The PEPs of Python 3.9 by pansa2 on Thursday May 28, 2020 @07:24PM (#23344308)
I 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?
Re:The PEPs of Python 3.9 by mark-r on Thursday May 28, 2020 @07:47PM (#23344517)
Don'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.
Re:The PEPs of Python 3.9 by dragonwriter on Thursday May 28, 2020 @11:15PM (#23345926)
Ternaries don't discard results that are generated, they are just special short-circuiting operators;

  x if y else z
Is effectively syntax sugar for:

  y and x or z
Nothing is discarded after evaluation, one of three arms is never evaluated, just as one of two arms of a common short-circuiting Boolean operator often (but not always) is not. That's essentially the opposite of executing and producing possible side effects and then discarding the results.
Re:The PEPs of Python 3.9 by owl57 on Thursday May 28, 2020 @10:36PM (#23345737)
What's the problem with ternaries?
Re:The PEPs of Python 3.9 by mark-r on Thursday May 28, 2020 @11:04PM (#23345876)
One of the two possible sub-expressions isn't used.
Re:The PEPs of Python 3.9 by mkl on Friday May 29, 2020 @01:29AM (#23346546)
It's also not evaluated. There is no discarding, so there would be no problem.
Re:The PEPs of Python 3.9 by nomel on Thursday May 28, 2020 @07:46PM (#23344515)
What is this “compile time” you speak of?
Re:The PEPs of Python 3.9 by pansa2 on Thursday May 28, 2020 @07:56PM (#23344605)
When the Python source code is compiled into bytecode.
Re:The PEPs of Python 3.9 by nomel on Tuesday June 02, 2020 @12:50AM (#23387577)
That 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.
Re:The PEPs of Python 3.9 by dragonwriter on Thursday May 28, 2020 @11:22PM (#23345970)
> Python is interpretted, not compiled, and completly dynamic. You cannot check much statically.

The existence of mypy and other static type checkers for Python disproves that; given their existence, warning of an expression producing a type other than “any” or strictly “None” was used in a position where it would neither be passed to another function or assigned to a variable that is used later should be possible. Heck, you could be stricter and only allow strictly “None” in that position.

Re:The PEPs of Python 3.9 by liveoneggs on Thursday May 28, 2020 @10:42PM (#23345769)
so what are these annoying pyc files about?
Re:The PEPs of Python 3.9 by pbowyer on Thursday May 28, 2020 @05:04PM (#23342599)
> And honestly, I would be rich if I got a dollar every time a student does this:

> msg.upper()

> Instead of:

> msg = msg.upper()

> And then call me to say it doesn't work.

On this, isn't the student's reasoning sensible? E.g. "If msg is a String object that represents my string, then calling .upper() on it will change (mutate) the value, because I'm calling it on itself"?

If the syntax was upper(msg) or to a lesser extent String.upper(msg) then the new-to-programming me would have understood more clearly that msg was not going to change. Have you any insights into what your students are thinking?

Re:The PEPs of Python 3.9 by pansa2 on Thursday May 28, 2020 @07:12PM (#23344188)
> String.upper(msg)

That was the original syntax [0], before the string functions became methods. I agree that a method more strongly implies mutation than a function does.

Also, for consistency with list methods like `reverse` (which acts in place) and `reversed` (which makes a copy), shouldn’t the method be called `uppered`?!

[0] https://docs.python.org/2/library/string.html#deprecated-str...

Re:The PEPs of Python 3.9 by moonchild on Thursday May 28, 2020 @07:58PM (#23344617)
'uppercased'
Re:The PEPs of Python 3.9 by pansa2 on Friday May 29, 2020 @02:06AM (#23346729)
Ah, of course.

Also, it looks like that’s the name that Swift uses.

Re:The PEPs of Python 3.9 by BiteCode_dev on Thursday May 28, 2020 @06:26PM (#23343624)
A student don't know anything about mutability, and since Python signatures are not explicit, there is no way to know they have to do that.

It's just something to be told. A design decision, like there are thousands to learn in IT, that you just can't guess.