Bulk Adding Methods Pythonically
Steven D'Aprano
steve at pearwood.info
Thu Jun 16 13:36:02 EDT 2016
On Fri, 17 Jun 2016 01:53 am, Julien Salort wrote:
> Ethan Furman <ethan at stoneleaf.us> wrote:
>
>> If that is all correct, then, as Random suggested, move that loop into
>> the class body and use locals() [1] to update the class dictionary.
>> Just make sure and delete any temporary variables.[
> [...]
>> [1] https://docs.python.org/3/library/functions.html#locals
>> Yes, returning the class namespace is a language gaurantee.
>
> But that says:
> "Note The contents of this dictionary should not be modified; changes
> may not affect the values of local and free variables used by the
> interpreter."
That only applies to locals() inside a function. The intent of locals()
inside a class is to be writable, and if the docs don't explicitly make
that guarantee, they should.
http://bugs.python.org/issue27335
--
Steven
More information about the Python-list
mailing list