Bug or Feature with (overriding) Class Variables?

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Sun Nov 16 15:38:53 EST 2003


On Sun, 16 Nov 2003 11:29:19 -0500, Eric Baker wrote:
> Basically what it boils down to, is that the operater "=" is doing
> different things.

No, the '=' operator is being consistent -- it always assigns a new
value to whatever is on the lhs (left-hand side).

What is inconsistent is the lhs object you are assigning to.

> With the expression:
> self.dict["bar-key"] = "bar-value"

Because you're not putting the dict itself on the lhs; you're referring
to one of its indexed values.

If, instead, you put:

    self.dict = { "foo": "bar-value" }

this *would* be an equivalent operation to:

    self.string = "bar-value"

> Wheras with this experession:
> self.string = "bar-string"
> You are actually creating a new instance, because strings are
> immutable the "=" operater does not modify the string but actually
> creates a new one within the current scope.

As would happen if you did the same thing to a dict.

-- 
 \          "If you get invited to your first orgy, don't just show up |
  `\             nude. That's a common mistake. You have to let nudity |
_o__)                                       'happen.'"  -- Jack Handey |
Ben Finney <http://bignose.squidly.org/>




More information about the Python-list mailing list