How to create new closure cell objects?

巧了我就是萌 提交于 2019-12-06 14:06:30

The simple way to make a closure cell would be to make a closure:

def make_cell(val=None):
    x = val
    def closure():
        return x
    return closure.__closure__[0]

If you want to reassign an existing cell's contents, you'll need to make a C API call:

import ctypes
PyCell_Set = ctypes.pythonapi.PyCell_Set

# ctypes.pythonapi functions need to have argtypes and restype set manually
PyCell_Set.argtypes = (ctypes.py_object, ctypes.py_object)

# restype actually defaults to c_int here, but we might as well be explicit
PyCell_Set.restype = ctypes.c_int

PyCell_Set(cell, new_value)

CPython only, of course.

in lambda:

def make_cell(value):
    fn = (lambda x: lambda: x)(value)
    return fn.__closure__[0]

got anwer from https://github.com/nedbat/byterun/blob/master/byterun/pyobj.py#L12

If you want an empty cell (which is what I found this question for) (One where referencing it raises a NameError: free variable '...' referenced before assignment in enclosing scope and accessing it's cell.cell_contents raises a ValueError: Cell is empty), you can make a value a local variable, but never let it be assigned:

def make_empty_cell():
    if False:
        # Any action that makes `value` local to `make_empty_cell`
        del value
    return (lambda: value).__closure__[0]

You can combine them like this:

_SENTINEL = object()

def make_cell(value=_SENTINEL):
    if value is not _SENTINEL:
        x = value
    return (lambda: x).__closure__[0]

So calling with no arguments returns an empty cell, and with any value, a cell with that value.

If you don't care about empty cells, you can do:

def make_cell(value):
    return (lambda: value).__closure__[0]

Note that it is func_closure in older Python 2, instead of __closure__.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!