473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

cell object dereferencing

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

Jul 18 '05 #1
8 3033
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
Jul 18 '05 #2
Peter Otten wrote:
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...


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.

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

Jul 18 '05 #3

"Jan Decaluwe" <ja*@jandecaluwe.com> wrote in message
news:3F**************@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
Jul 18 '05 #4
Terry Reedy wrote:
"Jan Decaluwe" <ja*@jandecaluwe.com> wrote in message
news:3F**************@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.

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

Jul 18 '05 #5
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

Jul 18 '05 #6

"Jan Decaluwe" <ja*@jandecaluwe.com> wrote in message
news:3F**************@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
Jul 18 '05 #7
Terry Reedy wrote:
"Jan Decaluwe" <ja*@jandecaluwe.com> wrote in message
news:3F**************@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.


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

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

Jul 18 '05 #8

"Jan Decaluwe" <ja*@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

Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
1
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say...
2
by: Chuck Hartman | last post by:
I've been trying to add an ImageButton object to a Calendar table cell, but so far I am unable to handle the Command event from that button in my form's code behind. Below is an example of what I...
2
by: Daniel Walzenbach | last post by:
Hi, I created an ASP.NET Datagrid where a single row can be selected by clicking anywhere on the row (according to...
18
by: Frank M. Walter | last post by:
Hello, I have made an small AddIn with udf for excel 2003. I use vs2003. The point of view is the function __T() I call it in excel sheet writing =__T() I am not able to set a value to a...
3
by: Rich | last post by:
Hello, If I want to update data displayed in a datagrideview/datagridview cell, how can I determine what cell I am updating? I am looking at the click event below, for example. Can I get...
13
by: Mike S | last post by:
I came across the following paragraph in the "Semantics" section for simple assignment in N1124 (C99 draft) and I'm wondering if I'm interpreting it right: 6.5.16.1p3: If the value being...
6
TMS
by: TMS | last post by:
This spreadsheet is almost done, but there is some functionality that is driving me nuts. For instance: a cell, for instance 'a0' is to have 'a0' as a string, but if something is entered like...
4
by: John Nagle | last post by:
I'm printing out each entry in "gc.garbage" after a garbage collection in DEBUG_LEAK mode, and I'm seeing many entries like <cell at 0x00F7C170: function object at 0x00FDD6B0> That's the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.