references and buffer()

Steve Holden steve at holdenweb.com
Sun Oct 8 21:33:13 CEST 2006


km wrote:
> Hi all,
> 
>     in the CPython implementation, it's the address where the object is
>     stored.  but that's an implementation detail. 
> 
> 
>  ok so can i point a vairiable to an address location just as done in C 
> language ?
>  >>> y = 'ATGCATGC'
>  >>> x = buffer(y)
>  >>> del(y)
>  >>> x
> <read-only buffer for 0xbf4cf0e0, size -1, offset 0 at 0xbf4cf240>
>  >>> print x
> ATGCATGC
> 
> now even when i delete y,  why is that x still exists ?

Because assignment is a *binding* of a name, in some namespace, to an 
object. x "still exists" because it hasn't been deleted. Because it is a 
reference to the object formerly bound to the name y, that object still 
also exists.

> thats true even in the case of vairable assignment which states it a a 
> reference !
>  >>> a = 10
>  >>> b = a
>  >>> del(a)
>  >>> b
> 10
> i always thought if u modify the referred variable/buffer object it 
> should be reflected in the referenced variables/buffers objects . am i 
> wrong ?

Yes, you are wrong. Think of Python names as pure references.

> does it mean that references  in python are not true references ?
> 
No, it doesn't mean that. Python bindings are exactly true references 
(think "pointer" in C), the storage for the referred objects is 
allocated from a heap and has a lifetime as long as the last reference 
to it and possibly, depending on the storage allocation and garbage 
collection strategy of the specific implementation, rather longer. Once 
all references have been deleted it can no longer be reached from inside 
Python code, however.

The original CPython implementation uses reference counting to control 
storage reclamation, but other implementations used other strategies.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list