by BiteCode_dev · 6y ago · 0 comments
For those situations, bytes() + memoryview() or bytearray() can be used, but then you are on your own.
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.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. 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?
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.
> 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?
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...
It's just something to be told. A design decision, like there are thousands to learn in IT, that you just can't guess.