Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cell object dereferencing

0 views
Skip to first unread message

Jan Decaluwe

unread,
Dec 9, 2003, 11:50:18 AM12/9/03
to
Is there a way to dereference a cell object (that is, get
the object that it references to) in Python?

Regards, Jan

--
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

Peter Otten

unread,
Dec 9, 2003, 11:19:09 AM12/9/03
to
Jan Decaluwe wrote:

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

I appreciate messages from the future, cryptic as they may be.
So: what cell in what prison?

Sorry, couldn't resist...

Peter

Jan Decaluwe

unread,
Dec 9, 2003, 12:49:39 PM12/9/03
to

Cell objects are afaik only documented briefly in the Python C API,
so I understand the question may sound cryptic.
For you knowledge, they exist today and are used to implement
nested scopes. The guy able to answer (I hope)
will probably understand the question immediately.

Terry Reedy

unread,
Dec 9, 2003, 1:59:01 PM12/9/03
to

"Jan Decaluwe" <j...@jandecaluwe.com> wrote in message
news:3FD5FD4A...@jandecaluwe.com...

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

[Background: a cell is an undefined internal implementation object used to
make nested scoping work as advertised. One might think of it as a means
for persisting cross-scope name-binding of objects in intermediate nested
scopes of nested functions. Alternatively, a cell is 'persistent read-only
shadow of an outer local'. For nested functions that access intermediate
locals, .func_closure is a tuple of 'cells'.]

Yes and no, depending on what you mean be 'dereference'. Within the nested
function, you 'dereference' the variable the same way you do any bound
ame -- write it! Outside the function, where the variable has no
conceptual existence, you can grab a cell from the func_closure tuple, but I
know of no way to access its value. Both repr() and str() return a <cell at
xxx: type at yyy> description. If you want a globally accessible value, use
a global variable.

Terry J. Reedy


Jan Decaluwe

unread,
Dec 9, 2003, 3:41:08 PM12/9/03
to
Terry Reedy wrote:
> "Jan Decaluwe" <j...@jandecaluwe.com> wrote in message
> news:3FD5FD4A...@jandecaluwe.com...
>
>>Is there a way to dereference a cell object (that is, get
>>the object that it references to) in Python?
>
>
> [Background: a cell is an undefined internal implementation object used to
> make nested scoping work as advertised. One might think of it as a means
> for persisting cross-scope name-binding of objects in intermediate nested
> scopes of nested functions. Alternatively, a cell is 'persistent read-only
> shadow of an outer local'. For nested functions that access intermediate
> locals, .func_closure is a tuple of 'cells'.]
>
> Yes and no, depending on what you mean be 'dereference'. Within the nested
> function, you 'dereference' the variable the same way you do any bound
> ame -- write it! Outside the function, where the variable has no
> conceptual existence, you can grab a cell from the func_closure tuple, but I
> know of no way to access its value.

This is what is mean - so I guess the answer is no.

>Both repr() and str() return a <cell at
> xxx: type at yyy> description. If you want a globally accessible value, use
> a global variable.

The background is that I am writing a small compiler that translates a
(small) subset of Python into another language. I would like to be able to
support free variables as they are likely to be useful in the kind of
code I'm targetting. However, I need to be able to inspect the corresponding
objects for their type etc. Conceptually this should be possible, just as
with globals and locals of functions and frames, but in practice it seems
it isn't - a real pity for which I hope to find a workaround.

Jan Decaluwe

unread,
Dec 10, 2003, 4:41:52 AM12/10/03
to
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.

Terry Reedy

unread,
Dec 10, 2003, 10:38:35 AM12/10/03
to

"Jan Decaluwe" <j...@jandecaluwe.com> wrote in message
news:3FD6EA60...@jandecaluwe.com...

> 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.:

> well you can ... 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)()

Cute, Samuele. If function.func_closure were writable (which it is not)
then I believe the last four lines could be condensed as the more readable

def cell_deref(cell):
acc0.func_closure = (cell,)
return acc0()

but since it is not, you instead make a new function that is a near copy of
acc0 but with (cell,) substituted as *its* func_closure.

Terry J. Reedy


Jan Decaluwe

unread,
Dec 10, 2003, 1:11:54 PM12/10/03
to

Aha, *that's* what the last argument is: func_closure. For those interested,
this is not yet in the documentation of module new, but it is documented
in new.function.__doc__.

Thanks a lot for this hack, it looks just what I need. I even start to
understand it, I believe. (Next thing I would like to understand is
how the hell you came up with this!)

Regards, Jan

Terry Reedy

unread,
Dec 10, 2003, 3:47:34 PM12/10/03
to

"Jan Decaluwe" <j...@jandecaluwe.com> wrote in message
>(Next thing I would like to understand is how the hell you came up with
this!)

I can't speak for Pedroni, but...
If you start with the two facts I initially stated -- cells are externally
accessible as .func_closure tuple members but their values are only
internally accessible -- and treat them as design guidelines (as Pedroni
did) rather than as deniers of possibility (my mistake), one is pretty much
lead to the conclusion that you need to attach the cells to a function that
reads and returns the value. This is Pedroni's template function. The
third fact -- that .func_closure is read-only, dictates the near-copy via
new instead of simple reuse of the template.

Terry

0 new messages