references and buffer()

Theerasak Photha hanumizzle at gmail.com
Sun Oct 8 14:59:56 EDT 2006


On 10/8/06, km <srikrishnamohan at gmail.com> wrote:
>
>  Hi all,
>
>  was looking at references  in python...
>  >>> a = 10
>  >>> b = a
>   >>> id(a)
>  153918788
>  >>>id(b)
>  153918788
>
>  where a and b point to the same id. now is this id an address ?

The id may be considered similar to an address in C, etc. slightly
different but conceptually close.

>  can one dereference a value based on address alone in python?

Not to my knowledge. Generally speaking, you wouldn't want to anyway.

>  is id similar to the address of a variable or a class ?

Exactly. Similar. Not the same, but similar.

>  read abt buffers as a refernce to a buffer object.
>  actually i tried passing list and dict types to buffer function, but only
> with string type i could createa buffer reference,
>  >>>y = 'GATCGTACC'
>  >>>x = buffer(y, 0,8)
>  >>> x
>  <read-only buffer for 0xbf4cd3b8, size 8, offset 2 at 0xbf4cf0e0>
>  >>>print x
>  TCGTACC
>  >>> id(y)
>  -1085484104
>  >>> id(x)
>  -1085476384
>
>  now the ids of y and x are not the same - why ?

You assigned two different object values to these names. In Python, a
name is just a name. It can point at any object. This relation becomes
very clear with mutable objects.

>>> x = {'foo':42, 'bar':69}
>>> id(x)
1076761980
>>> y = x
>>> y['baz'] = 36
>>> id(y)
1076761980
>>> y
{'baz': 36, 'foo': 42, 'bar': 69}
>>> x
{'baz': 36, 'foo': 42, 'bar': 69}

When I wrote y = x, all I did was make the variable y point to the
dictionary object x is also pointing at. Hence they point to the same
object. Things would be different if I decided to copy an object
instead:

>>> x = {'foo':42, 'bar':69}
>>> import copy
>>> y = copy.deepcopy(x)
>>> y['baz'] = 36
>>> id(x)
1076761164
>>> id(y)
1076890180
>>> x
{'foo': 42, 'bar': 69}
>>> y
{'baz': 36, 'foo': 42, 'bar': 69}

Since your name is Sri Krishna, an avatar of Vishnu, the concept
should be familiar. Variables are like avatars; they represent the
object (whether this is a humble dictionary or Vishnu the Preserver)
for the user, and serve as a bridge between the computer and the user,
as an avatar of Vishnu is a bridge between the physical world and the
supernatural.

Dasha Avatar -- one god. Think about it.

>  what exactly are buffer object types ? and how can i instantiate them ?

If you want to get a line of user input, the basic function is raw_input().

-- Theerasak



More information about the Python-list mailing list