references and buffer()

2006-10-08 Thread km

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 ? 
can one dereference a value based on address alone in python?
is id similar to the address of a variable or a class ? 

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 ? 
In which situatons are buffers used against the strings ? 
can i create a write buffer instead of readonly buffer ?
what exactly are buffer object types ? and how can i instantiate them ? 

regards,
KM

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: references and buffer()

2006-10-08 Thread Fredrik Lundh
km 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 ?

no, it's the object identity, and all it tells you is that both names 
point to the same object.

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

no.

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

in the CPython implementation, it's the address where the object is 
stored.  but that's an implementation detail.

/F

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: references and buffer()

2006-10-08 Thread Theerasak Photha
On 10/8/06, km [EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: references and buffer()

2006-10-08 Thread km
Hi all,
in the CPython implementation, it's the address where the object isstored.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 ? 
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 ? 
does it mean that references in python are not true references ? 

regards,
KM


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: references and buffer()

2006-10-08 Thread Carl Friedrich Bolz
Fredrik Lundh wrote:
[snip]
 is id similar to the address of a variable or a class ?

 in the CPython implementation, it's the address where the object is
 stored.  but that's an implementation detail.

Just as an obscure sidenote, in PyPy it is ~address some of the time.
This is due to the fact that since PyPy can use the Boehm garbage
collector. The Boehm collector is conservative and therefore has to
assume that everything in RAM could be a pointer. Now if somebody stores
the id of an object the GC could not distinguish this from a real pointer
if id returned the address, which would keep the object alive.

/sidenode

Cheers,

Carl Friedrich Bolz

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: references and buffer()

2006-10-08 Thread Fredrik Lundh
km wrote:

 ok so can i point a vairiable to an address location just as done in C 
 language ?

no.  there are no C-style variables in Python; just objects and names 
bound to objects.  Python variables are names, not memory locations.

   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 it's an object.

 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 ?

reset your brain:

 http://effbot.org/zone/python-objects.htm

/F

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: references and buffer()

2006-10-08 Thread Theerasak Photha
On 10/8/06, km [EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: references and buffer()

2006-10-08 Thread Steve Holden
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: references and buffer()

2006-10-08 Thread km
Hi all,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. Notmuch of a difference; I haven't seen buffer objects yet (I am also newto Python), but the initialization for the buffer probably copieswhatever is in y somewhere.

that means when u refer to an object with different names
(variable), it referes to the same object- fine. but is it
that the original object stays in memory until it is Garbage
Collected ?
is it that del() deletes the link of variable to the object
and not the object ? and thats why u can access it from other variables
? 

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 avatarspassed from the physical world; it is no different with objects inPython.
vishnu analogy is a bit complicated as it is a manifestation of
divine energy in terms of earthly object(avatar). Its clearly not a
reference. each avatar is himself (vishnu). It is the same energy
people around have too (coz of manifestation). ofcourse they dont
realise coz of ego (id in python) and so the object class (divine
energy) is the same - unlike python where we have different classes
derived from object class.
IOWa -- 10b -/Delete the 'a' reference and:b -- 10

got it!

What is a little different is this: if there are no references left toan object (such as variables), the object the references point to will
eventually be deleted. Variables are one way to have a reference to anobject. References to an object may also exist in a list, hash, orother data type.
so the object exists until there are no references to it and will be Garbage Collected immediately? 
regards,
KM

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: references and buffer()

2006-10-08 Thread Theerasak Photha
On 10/8/06, km [EMAIL PROTECTED] wrote:
 Hi all,


  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.

  that means when u refer to an object  with different names (variable), it
 referes to the same object- fine. but  is it that  the original object stays
 in memory until it is Garbage Collected ?

Exactly.

  is it that  del() deletes  the link of variable to the object and not the
 object ? and thats why u can access it from other variables ?

Exactly.

  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.

  vishnu analogy is a bit complicated as  it is a manifestation of divine
 energy in terms of earthly object(avatar). Its clearly not a reference. each
 avatar is himself (vishnu). It is the same energy people around have too
 (coz of manifestation). ofcourse they dont realise coz of ego (id in python)
  and so the object class (divine energy) is the same - unlike python where
 we have different classes derived from object class.

Congratulations, you understand both Hinduism and Python better than I
do now.  :) c.f.
http://www.swami-krishnananda.org/brdup/brhad_III-09.html

Kati references, Yajnavalkya, iti?

(It is worth noting that a subclass is an instance of its superclass,
both in terms of interface and implementation.)

  so the object exists until there are no references  to it and  will be
 Garbage Collected  immediately?

Python uses two garbage collection schemes together. It uses reference
counting (when number of references goes to zero, remove object from
memory) and mark-and-sweep (comb through process memory methodically
looking for objects that are no longer accessible). This is what
allows it to collect cyclic structures, such as trees whose nodes
links to their parents and vice versa.

GC intercedes at various intervals when convenient. I don't think it
would be immediate though.

-- Theerasak
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: references and buffer()

2006-10-08 Thread km

Hi all, 
Congratulations, you understand both Hinduism and Python better than Ido now.:) 
c.f.http://www.swami-krishnananda.org/brdup/brhad_III-09.htmlKati references, Yajnavalkya, iti?
the answer lies in a single line as pronounced by sri adi sankaracharya - 
aham bramhasmi sivoha sivoham  , which is still not implemented in
python. infact not in any other language! which is a limitatin of
computers (and comp languages) - they simply arent intelligent.
 aham, tvam sah: cha madhye kim bhedam bhavati ? bhedam nasti !
Python uses two garbage collection schemes together. It uses referencecounting (when number of references goes to zero, remove object from
memory) and mark-and-sweep (comb through process memory methodicallylooking for objects that are no longer accessible). This is whatallows it to collect cyclic structures, such as trees whose nodeslinks to their parents and vice versa.

why is that python doesnt implement direct memory addressing provided a reference to an object exists ?
GC intercedes at various intervals when convenient. I don't think itwould be immediate though.
what is the interval and what is its effect on the performance of python interpreter ?
regards,
KM


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: references and buffer()

2006-10-08 Thread Fredrik Lundh
km wrote:

 why is that python doesnt implement direct memory addressing provided a 
 reference to an object exists ?

because Python is a portable high-level language.  if you want 
assembler, you shouldn't use Python.

did you read the reset your brain article ?

/F

-- 
http://mail.python.org/mailman/listinfo/python-list