Closures in leu of pointers?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jun 29 14:45:30 EDT 2013


On Sat, 29 Jun 2013 04:21:46 -0700, cts.private.yahoo wrote:

> Thank you.  You reminded me of the (weak) workaround of using arrays 

I think you mean lists, rather than arrays. Python does have an array 
type, but it is much more restricted.

If you want an indirect reference to a value, the simplest ways are via a 
object such as a list, dict or mutable object with named attributes. 
That's not really a "weak workaround". In C you would dereference a 
pointer, in Python you "dereference" a list:

ptr = &obj; 
value = *ptr;

becomes:

ptr[0] = obj
value = ptr[0]


although don't push the analogy too far, Python lists aren't pointers in 
the C sense.

What Python doesn't have -- and it doesn't seem to me that it could have, 
without support from the interpreter, is a simple way to indirectly refer 
to another *name*, rather than another object.


> and
> confirmed my suspicion that I although I can read the variable, I won't
> be able to write to it.  I still don't understand why not, though...

In Python 2, you simply can't because the interpreter doesn't support it.

 
> As for python 3 ... "nonlocal"?  I see I'm not alone in picking
> obnoxious names ...

Huh?

You have "global" for global names. Python require declarations for local 
names, but if it did it would probably use "local". What name would you 
pick to declare names nonlocal other than "nonlocal"?


-- 
Steven



More information about the Python-list mailing list