references and buffer()
Theerasak Photha
hanumizzle at gmail.com
Sun Oct 8 15:24:41 EDT 2006
On 10/8/06, km <srikrishnamohan at gmail.com> 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 ?
Say that you copy the contents of file foo into file bar and delete
the original foo. Of course file bar still exists in this case. Not
much of a difference; I haven't seen buffer objects yet (I am also new
to Python), but the initialization for the buffer probably copies
whatever is in y somewhere.
> 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
You didn't modify the object that the variable /refers to/.
Furthermore, numbers are immutable anyway. To continue with the Hindu
god analogy, Vishnu did not cease to exist when any of his avatars
passed from the physical world; it is no different with objects in
Python.
IOW
a --> 10
b -/
Delete the 'a' reference and:
b --> 10
Or:
>>> class god(object):
... pass
...
>>> vishnu = god()
>>> matsya = vishnu
>>> kurma = vishnu
>>> varaha = vishnu
>>> narasimha = vishnu
>>> # Etc
...
>>> del narasimha
>>> matsya
<__main__.god object at 0x402e3c6c>
>>> kurma
<__main__.god object at 0x402e3c6c>
What is a little different is this: if there are no references left to
an object (such as variables), the object the references point to will
eventually be deleted. Variables are one way to have a reference to an
object. References to an object may also exist in a list, hash, or
other data type.
>>> a = 'foo'
>>> b = [1,2,3,a]
>>> del(a)
>>> b
[1, 2, 3, 'foo']
> am i wrong ?
> does it mean that references in python are not true references ?
Python is object-oriented in either sense of the word (think Lisp
sense then think Smalltalk sense). These are true references.
-- Theerasak
More information about the Python-list
mailing list