Pass-by-reference : Could a C#-like approach work in Python?

Bengt Richter bokr at oz.net
Fri Sep 12 01:23:37 CEST 2003


On Thu, 11 Sep 2003 16:44:28 -0400, "Terry Reedy" <tjreedy at udel.edu> wrote:

>
>"Stephen Horne" <$$$$$$$$$$$$$$$$$@$$$$$$$$$$$$$$$$$$$$.co.uk> wrote
>in message news:66t0mv410it5ftvjokfpkov0v029trv6lp at 4ax.com...
>
>> Actually I'm sick to death of the old 'your just trying to turn
>Python
>> into X' argument whenever I suggest that *maybe* Python might learn
>a
>> trick from another language. [etc]
>
>This seems to be a predictible c.l.py response to proposals.  I got
>similar accusations several years ago, which made me similarly sick,
>when I proposed the addition of list.pop() (which Guido did, in more
>generalized form, about a year later).
>
>My main objection was/is that expressed by Michael Chermside today:
>this would complexify a simplicity that I consider a good feature of
>Python.  Reading Bengt Richter's lastest response, also today, I
>realize that 'ref x' would have to have much the same effect of Lisp's
>FEXPR defun keyword -- of automatically quoting rather than evaluating
>args -- although on just one rather than all.  Perhaps you should be
>accused of 'trying to Lispify Python' -- or perhaps not ;-).
>

If we had a wrapper for rebinding targets, then maybe such a wrapper could
be passed to functions and used in a uniform way. E.g., faking it with real python:

 >>> class GAW(object): # Generalized Access Wrapper
 ...     def __init__(self, thing, key):
 ...         self.thing=thing
 ...         self.key=key
 ...     def _getv(self):
 ...         try: return self.thing[self.key]
 ...         except: pass
 ...         try: return getattr(self.thing, self.key)
 ...         except: pass
 ...         raise KeyError,'%r is not key or attribute of %r'%(self.thing, self.key)
 ...     def _setv(self, v):
 ...         try: self.thing[self.key]=v
 ...         except:
 ...             try: setattr(self.thing, self.key, v)
 ...             except: raise KeyError,'%r is not key or attribute of %r'%(self.thing, self.ke 
 ...     value = property(_getv,_setv)
 ...
 >>> a= ['foo', [23]]

Oops, I should have called this Inc ;-)

 >>> def foo(aw):
 ...     aw.value = aw.value + 1
 ...
 >>> aaw = GAW(a[1], 0)
 >>> aaw.value
 23
 >>> foo(aaw)
 >>> a
 ['foo', [24]]
 >>> aaw.value
 24
 >>> z=100
 >>> foo(GAW(globals(),'z'))
 >>> z
 101

Maybe something could be done in C and with compiler support to implement safe access
via deferral of the final access tail term of a target expression, using an object with an
apparent value property that works more or less as above -- but so you could spell it
e.g., awrap(z) or awrap(obj.x) or awrap(a[1][0]) by some magic ;-)

The compiler would have to know about awrap, so it could separate the final access mechanism
in the expression and create the code to create the appropriate access object...
(a lot of hand waving, but maybe not impossible?)

Regards,
Bengt Richter




More information about the Python-list mailing list