Gary Herron <gher...@islandtraining.com> writes:

> But now you are not listening to what people are telling you.  It has
> *nothing* to do with the mutability/immutability of the integer and the list
> your two classes create.

No! Did you run the code he posted? The immutability makes all the
difference.


> The difference is this:

>    For C1: You create 10 instances of C1. Each one creates its own
> count, and a list variables, and manipulates them calls to inc and
> val. Then each on is discarded as you go through the next pass on the
> outer loop.

>    For C2; You create 10 instances of C2, but these 10 instances each
> manipulate values created once in the class itself. The values
> manipulated by one instance of C2 in one pass through the loop are not
> affected when, on the next pass through the loop, that instance is
> destroyed and another instance is created.

If you try it, you will see that this isn't true for C2, in the case of
the immutable object. That's because given this code:

  class Foo(object):
      x = 0

      def inc(self):
          self.x += 1

when inc is called, it creates an 'x' attribute on the instance, even
though one didn't exist before, and it doesn't change the value of the
class attribute.

If 'x' were a list, the new instance attribute would refer to the same
object as the class attribute, so it wouldn't make much difference. But
when the original class attribute was an immutable object, they become
'decoupled'.


I think this code is in poor taste: it's clear that it will confuse
people (which is what Maxim was asking about in the first place).

-M-
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to