Beginner question - How to effectively pass a large list

Donn Cave donn at u.washington.edu
Tue Dec 16 19:01:26 CET 2003


In article <brmf9g$t59$1 at newshost.mot.com>,
 "J.R." <j.r.gao at motorola.com> wrote:

> Thanks for the response.
> 
> I got following conclusion after reading your reply and other documents:
> 
> Actually, the python is passing the identity (i.e. memory address) of each
> parameter, and it will bind to a local name within the function.

That should work.  This notion of binding a object to a name or
data structure is all over Python, as you have probably noticed,
and it will be time well spent if you experiment with it a little.

For example, how could you verify that arrays are passed without
copying?  Unless you are unusually dense for a programmer or have
no access to a Python interpreter, that could be no more than a
couple of minutes work.  As penance for having failed to do this,
I assign a more mysterious problem to you:

   def f(d=[]):
      d.append(0)
      print d
   f()
   f()

Explain results.  When is d bound?

Here's an easier one:

   a = {'z': 0}
   b = [a] * 4
   b[0]['z'] = 1
   print b

If the result is anywhere near a surprise to you, then you would
do well to stick to this line of inquiry if you want to do anything
more than the most trivial Python programming.

   Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list