Variable Scope 2 -- Thanks for 1.

Shalabh Chaturvedi shalabh at gameboard.org
Sat Jan 10 22:05:17 EST 2004


Duncan Booth wrote:

> Be careful. The integer object is actually altered, at least in so far as
> its reference count (which is part of the object) is changed:
> 
>>>> import sys
>>>> sys.getrefcount(29)
> 11
>>>> age = 29
>>>> sys.getrefcount(29)
> 12
>>>> 
> 

Off the orig topic, but I think this only happens for small numbers
(optimization - only one object exists for small numbers in Python).

>>> a = 101
>>> sys.getrefcount(101)
2
>>> b = 101
>>> sys.getrefcount(101)
2
>>>

In other words:

>>> a = 29
>>> b = 29
>>> a is b
True
>>> a = 101
>>> b = 101
>>> a is b
False

Of course, 'is' isn't useful when comparing int objects. For other
operations, the semantics are not affected anyway as int objects are
immutable.

--
Shalabh



More information about the Python-list mailing list