Closures in leu of pointers?

Peter Otten __peter__ at web.de
Sat Jun 29 08:12:57 EDT 2013


cts.private.yahoo at gmail.com wrote:

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

tous chez...

> Alas, one reason it's a weak workaround is that it doesn't work - at
> least, not how I wish it would:

> $ cat ptrs
> 
> x = 34
> 
> def p1 (a1):
> 
>     a1[0] += 12
>     
> p1 ([x])
> 
> print (x)
> 
> $ python ptrs
> 34

That doesn't work with 'nonlocal' either. 

You can compare Python's names with (automatically dereferenced and 
garbage-collected) pointers, but there is no way to have a name refer to 
another name. The C-like

f(var *value)
{
    value = ...
}

is the only way to pass a variable in Python. There is no way to emulate

f(var **value)
{
   *value = ...
}

When this came up a few days ago I linked to

http://learntofish.wordpress.com/2012/01/09/call-by-object-reference-call-by-sharing/

but I was actually looking for the classic, so there:

http://python.net/~mwh/hacks/objectthink.html

PS: If you're reading this and love the French language -- I am deeply sorry 
for the pain I'm causing you...




More information about the Python-list mailing list