Objects in Python

alex23 wuwei23 at gmail.com
Thu Aug 23 23:17:14 EDT 2012


On Aug 24, 11:34 am, Chris Angelico <ros... at gmail.com> wrote:
> On Fri, Aug 24, 2012 at 10:36 AM, Roy Smith <r... at panix.com> wrote:
> > Even id() thinks they're the same thing:
> >>>> id(a)
> > 1755402140
> >>>> id(globals()["a"])
> > 1755402140
>
> Ah, no. What you have there is actually id(4) and nothing to do with a at all.

Well, nothing expect for the fact that he's demonstrating Python
references and how they bind to objects. Sure, id() isn't doing any
lookup, but that's missing the point.

> > But, notice what happens if I now assign something new to a:
>
> >>>> a = 123
> >>>> id(a)
> > 1755403176
>
> > The id has changed!  Now, we all know that the id of an object is its
> > memory address (that's not guaranteed, but in the standard C
> > implementation of Python, that's what it is).
>
> And you now have id(123) - of course, it's possible for there to be
> two integer objects with the value 123, but what I'm emphasizing is
> that you're not looking at a here.

But Roy's point was that referring to 'a' as a 'variable' makes no
sense, as it's not an allocated piece of memory. In fact, he even said
"the id of an object" and not "the id of 'a'" so I'm not entirely sure
what you're objecting to here. You don't need to emphasise anything as
_that was the point_: you're _not_ looking at 'a' _ever_, you're only
ever looking at the object to which it refers.



More information about the Python-list mailing list