git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

I'm wrong or Will we fix the ducks limp?


On Tue, Jun 7, 2016, at 08:32, Antoon Pardon wrote:
> > Here's a thought experiment for you. Suppose in Python 3.6, Guido announces 
> > that Python will support a form of high-level pointer (not the scary, dangerous 
> > low-level pointer of C) called "reference". There will be a dereference 
> > operator, ^, and a "reference to" operator, @. We'll be able to treat 
> > references as first-class values:
> 
> That makes very little sense in python.  

Why not? If you prefer, think of it something like:

class ItemPtr:
   def __init__(self, obj, key):
      self.obj = obj
      self.key = key
   def ___setvalue___(self, value):
      self.obj[self.key] = value
   def ___getvalue___(self)
      return self.obj[self.key]

@(foo[bar]) returns ItemPtr(foo, bar)
@baz where bar is a global variable returns ItemPtr(globals(), 'baz')
@quux where quux is local returns a cell object (any local that is used
in such an expression becomes a cell variable as with any local that is
used in a closure), and the cell type shall have these methods added to
it.
@(foo.bar) returns AttrPtr(foo, 'bar') where AttrPtr is a similarly
defined type.

We could even support a kind of "pointer arithmetic" for ItemPtr to
list-like containers:

def __add__(self, offset):
    return ItemPtr(self.obj, self.key + offset)
def __getitem__(self, offset):
    return self.obj[self.key + offset]