This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Allow setting cell value
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: lisroach Nosy List: Jim Fasarakis-Hilliard, benjamin.peterson, lisroach, ncoghlan, pitrou, rhettinger, serhiy.storchaka, terry.reedy, vstinner
Priority: normal Keywords:

Created on 2017-05-26 16:17 by pitrou, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 1840 merged lisroach, 2017-05-28 05:00
PR 3668 merged vstinner, 2017-09-20 13:01
Messages (10)
msg294554 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-05-26 16:17
There are use cases for setting a cell value.  One such use case is for (un)pickling recursive closures (see heroic workaround here: ).  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_contents` could become a read/write property...
msg294573 - (view) Author: Lisa Roach (lisroach) * (Python committer) Date: 2017-05-27 01:14
This idea makes sense to me. I'll see if I can put together the code for it.
msg294577 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-05-27 03:08
+1  I don't see any reason this can't be writeable.
msg294583 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-05-27 06:14
+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)
msg295057 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-06-02 22:00
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

set_cell = outer()
print(set_cell.__closure__[0].cell_contents)  # None
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?
msg295058 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-06-02 22:01
There's already a documentation change to the PR, you can leave your comments there.
msg295416 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-06-08 11:43
New changeset 64505a1f6c0af4574e17e823b27ffe24eca44df5 by Serhiy Storchaka (Lisa Roach) in branch 'master':
bpo-30486: Allow setting cell value (#1840)
msg295417 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-06-08 11:45
Thank you for your contribution Lisa!
msg295476 - (view) Author: Lisa Roach (lisroach) * (Python committer) Date: 2017-06-09 01:22
Thank you for you guidance, Serhiy!

On Thu, Jun 8, 2017 at 4:45 AM, Serhiy Storchaka <>

> Serhiy Storchaka added the comment:
> Thank you for your contribution Lisa!
> ----------
> resolution:  -> fixed
> stage: patch review -> resolved
> status: open -> closed
> _______________________________________
> Python tracker <>
> <>
> _______________________________________
msg302617 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-09-20 13:54
New changeset 0ad05c32cc41d4c21bfd78b9ffead519ead475a2 by Victor Stinner in branch 'master':
bpo-30486: Make cell_set_contents() symbol private (#3668)
Date User Action Args
2022-04-11 14:58:46adminsetgithub: 74671
2017-09-20 13:54:17vstinnersetnosy: + vstinner
messages: + msg302617
2017-09-20 13:01:39vstinnersetpull_requests: + pull_request3658
2017-06-09 02:26:34gvanrossumsetnosy: - gvanrossum
2017-06-09 01:22:47lisroachsetmessages: + msg295476
2017-06-08 11:45:01serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg295417

stage: patch review -> resolved
2017-06-08 11:43:28serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg295416
2017-06-02 22:01:10pitrousetmessages: + msg295058
2017-06-02 22:00:27terry.reedysetnosy: + terry.reedy

messages: + msg295057
stage: needs patch -> patch review
2017-05-28 05:00:15lisroachsetpull_requests: + pull_request1925
2017-05-27 06:14:28ncoghlansetmessages: + msg294583
2017-05-27 03:09:04rhettingersetassignee: lisroach
2017-05-27 03:08:48rhettingersetmessages: + msg294577
2017-05-27 01:14:17lisroachsetnosy: + lisroach
messages: + msg294573
2017-05-26 17:03:20Jim Fasarakis-Hilliardsetnosy: + Jim Fasarakis-Hilliard
2017-05-26 16:17:01pitroucreate