On Sun, Dec 12, 2021 at 1:23 PM Steven D'Aprano <st...@pearwood.info> wrote: > > TL;DR: it is hard, but not impossible, to set function `__globals__` > dunder to a ChainMap. Let's make it easier! > > Attempt 1: set __globals__ directly. > > >>> func.__globals__ = ns > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > AttributeError: readonly attribute > > I think we should have: > > - allow func.__globals__ to be writable;
CodeType has a replace() method that returns a new code object with identical attributes but for the ones you asked to change. If making __globals__ writable causes problems, a FunctionType.replace() method would probably be a good alternative. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/EQ5LEXW7IU24UVDJ3XHC4SEXYCZFK5CM/ Code of Conduct: http://python.org/psf/codeofconduct/