cell object dereferencing

Jan Decaluwe jan at jandecaluwe.com
Wed Dec 10 04:41:52 EST 2003


Jan Decaluwe wrote:
> Is there a way to dereference a cell object (that is, get
> the object that it references to) in Python?

I got the following response from Samuele Pedroni. I'll repost this
first, and then start thinking about it :-)

--

[I was reading the news group through google, feel free to repost this]

well you can write a C extension or use this hack (it's a huge hack but it is safe
and does the trick):

def proto_acc(v=None):
  def acc():
    return v
  return acc
acc0 = proto_acc()
import new
make_acc = lambda cell: (new.function (acc0.func_code,acc0.func_globals,'#cell_acc',acc0.func_defaults,(cell,)))

def cell_deref(cell):
  return make_acc(cell)()

# usage

def g(x,y):
   def f(): return x,y
   return f

f=g(1,2)

f_cells_by_name = dict(zip(f.func_code.co_freevars,f.func_closure))

print cell_deref(f_cells_by_name['x'])
print cell_deref(f_cells_by_name['y'])

regards.


-- 
Jan Decaluwe - Resources bvba - http://jandecaluwe.com
Losbergenlaan 16, B-3010 Leuven, Belgium
    Bored with EDA the way it is? Check this:
    http://jandecaluwe.com/Tools/MyHDL/Overview.html





More information about the Python-list mailing list