You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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_contents could become a read/write property...
+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,
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?
Activity
pitrou commentedon May 26, 2017
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...lisroach commentedon May 27, 2017
This idea makes sense to me. I'll see if I can put together the code for it.
rhettinger commentedon May 27, 2017
+1 I don't see any reason this can't be writeable.
ncoghlan commentedon May 27, 2017
+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)
terryjreedy commentedon Jun 2, 2017
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 inner
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?
pitrou commentedon Jun 2, 2017
There's already a documentation change to the PR, you can leave your comments there.
serhiy-storchaka commentedon Jun 8, 2017
New changeset 64505a1 by Serhiy Storchaka (Lisa Roach) in branch 'master':
bpo-30486: Allow setting cell value (bpo-1840)
64505a1
serhiy-storchaka commentedon Jun 8, 2017
Thank you for your contribution Lisa!
lisroach commentedon Jun 9, 2017
Thank you for you guidance, Serhiy!
On Thu, Jun 8, 2017 at 4:45 AM, Serhiy Storchaka <report@bugs.python.org>
wrote:
vstinner commentedon Sep 20, 2017
New changeset 0ad05c3 by Victor Stinner in branch 'master':
bpo-30486: Make cell_set_contents() symbol private (bpo-3668)
0ad05c3