[Python-Dev] Examples for PEP 572 (original) (raw)

Steven D'Aprano steve at pearwood.info
Wed Jul 4 12:02:47 EDT 2018


On Wed, Jul 04, 2018 at 10:20:35AM -0400, David Mertz wrote:

Hmmm... I admit I didn't expect quite this behavior. I'm don't actually understand why it's doing what it does.

>>> def myfun(): ... print(globals().update({'foo', 43}), foo)

Try it with a dict {'foo': 43} instead of a set :-)

... >>> myfun() Traceback (most recent call last): File "", line 1, in File "", line 2, in myfun TypeError: cannot convert dictionary update sequence element #0 to a sequence

I think Chris meant to try it inside a function using locals() rather than globals.

That said, this is a silly game either way. And even though you CAN (sometimes) bind in an expression pre-572, that's one of those perverse corners that one shouldn't actually use.

Still, it is sometimes useful to explore scoping issues by using globals() and/or locals() to see what happens. But I would really hesitate to use them in production unless I was really careful.

-- Steve



More information about the Python-Dev mailing list