On Mon, 2 Jan 2006 09:37:45 +0900, Jonathan L.
Can’t change the value of self (SyntaxError)
self = round_to *args
Why doesn’t it work? What can I do instead?
One way of thinking about reassigning self is if you imagine in a
self *= 2
That would amount to saying, if we did 1.double,
1 becomes 2
which would mean that 1+1 would thereafter be 4, and that would be bad.
What we need is not the value 1 to become 2, but to get the value 2 back
say 1.double. So we say
or, since “return” is implied
In general in OO, an object can change its contents, but it cannot
“self”. What should happen in this program?
one = 1
another_one = 1
When we double one, if the self*=2 thing worked, the ??? asserts would
4. We might be able to understand another_one being 2, but to have all
occurrences of 1+1 in the program turn into 4 … that would be bad.
Certain objects are called “value objects”, and the idea is that like
they never change value. For many objects it is a design choice whether
to make them value objects, even if they are quite complex. For example,
articles I’m currently writing on extended set theory on my site, I’m
the direction of making my sets value objects. They are essentially
large collections of records (like relational database relations), and
apps think of relations as mutable. I’m going to make a different
namely that any set, once created, is a constant. It’ll be interesting