Repository navigation
Allow setting cell value #74671
Description
Activity
There are use cases for setting a cell value. One such use case is for (un)pickling recursive closures (see heroic workaround here: https://github.com/cloudpipe/cloudpickle/pull/90/files#diff-d2a3618afedd4e124c532151eedbae09R74 ). Other use cases may include tinkering around and general education value.
There also doesn't seem to be, AFAICS, any counter-indication to being able to do so. It's already possible in C using PyCell_Set(), which is a public API. It just lacks an API in Python land. For example
cell_contentscould become a read/write property...- added3.7 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement
on May 26, 2017 This idea makes sense to me. I'll see if I can put together the code for it.
+1 I don't see any reason this can't be writeable.
+1 from me, as this also has potential value in testing use cases and in dealing with some of the consequences of the zero-arg super() design (i.e. it will make the injected __class__ cell writable)
A possible counter-indication would be if setting a cell could cause a crash, as opposed to a mysterious exception. Since 3.x already allows writing any object to a cell from python code,
def outer():
cell = None
def inner(ob):
nonlocal cell
cell = ob # rebinds <hidden-cell>.cell_contents
return innerset_cell = outer() print(set_cell.__closure__[0].cell_contents) # None set_cell('something') print(set_cell.__closure__[0].cell_contents) # 'something'
making "cell.cell_contents = 'something'" legal should not enable more crashes.
I think that "function.__closure__[i].cell_contents = object" is perhaps not a good idea for production code, but I think it falls within the realm of 'consenting adults code' for the other uses suggested above. How should it be documented?
There's already a documentation change to the PR, you can leave your comments there.
Thank you for your contribution Lisa!
Thank you for you guidance, Serhiy!
On Thu, Jun 8, 2017 at 4:45 AM, Serhiy Storchaka <report@bugs.python.org>
wrote:Serhiy Storchaka added the comment:
Thank you for your contribution Lisa!
----------
resolution: -> fixed
stage: patch review -> resolved
status: open -> closed
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue30486\>
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: