Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8698

Re: Inexplicable behavior in simple example of a set in a class

References <2dc52e22-5c0f-4e24-8197-df616db1a42c@u2g2000yqb.googlegroups.com> <mailman.566.1309644846.1164.python-list@python.org> <6441039f-f226-4632-8e74-2b6415bf914e@n28g2000vbs.googlegroups.com>
Date 2011-07-03 10:46 +1000
Subject Re: Inexplicable behavior in simple example of a set in a class
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.571.1309653963.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Jul 3, 2011 at 8:23 AM, Saqib Ali <saqib.ali.75@gmail.com> wrote:
> So just out of curiosity, why does it work as I had expected when the
> member contains an integer, but not when the member contains a set?

It's not integer vs set; it's the difference between rebinding and
calling a method. It's nothing to do with object orientation; the same
happens with ordinary variables:

>>> a=b=1
>>> a,b
(1, 1)
>>> a=2
>>> a,b
(2, 1)

>>> c=d=[]
>>> c,d
({}, [])
>>> c.append("Test")
>>> c,d
(['Test'], ['Test'])

But:
>>> c=['Foobar']
>>> c,d
(['Foobar'], ['Test'])

When you do a=2 or c=['Foobar'], you're rebinding the name to a new
object. But c.append() changes that object, so it changes it
regardless of which name you look for it by.

Chris Angelico

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Inexplicable behavior in simple example of a set in a class Saqib Ali <saqib.ali.75@gmail.com> - 2011-07-02 14:59 -0700
  Re: Inexplicable behavior in simple example of a set in a class Chris Rebert <clp2@rebertia.com> - 2011-07-02 15:14 -0700
    Re: Inexplicable behavior in simple example of a set in a class Saqib Ali <saqib.ali.75@gmail.com> - 2011-07-02 15:23 -0700
      Re: Inexplicable behavior in simple example of a set in a class Chris Rebert <clp2@rebertia.com> - 2011-07-02 16:25 -0700
      Re: Inexplicable behavior in simple example of a set in a class Chris Angelico <rosuav@gmail.com> - 2011-07-03 10:46 +1000
      Re: Inexplicable behavior in simple example of a set in a class Chris Rebert <clp2@rebertia.com> - 2011-07-02 18:07 -0700
      Re: Inexplicable behavior in simple example of a set in a class Chris Angelico <rosuav@gmail.com> - 2011-07-03 11:14 +1000
  Re: Inexplicable behavior in simple example of a set in a class Peter Otten <__peter__@web.de> - 2011-07-03 00:22 +0200
  Re: Inexplicable behavior in simple example of a set in a class Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-03 12:25 +1000

csiph-web