[Python-ideas] Unpack of sequences
M.-A. Lemburg
mal at egenix.com
Wed Aug 29 20:50:47 CEST 2012
Steven D'Aprano wrote:
> On 30/08/12 04:16, M.-A. Lemburg wrote:
>
>>>>> d = dict(a=1, b=2, c=3)
>>>>> locals().update(d)
>>>>> a
>> 1
>>>>> b
>> 2
>>>>> c
>> 3
>>
>> Not that I'd recommend doing this, but it's possible :-)
>
>
> Try it inside a function.
>
>
> py> def test():
> ... d = dict(a=1, b=2, c=3)
> ... locals().update(d)
> ... print a
> ...
> py> test()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 4, in test
> NameError: global name 'a' is not defined
Yeah, that's because functions use fast locals, which locals()
only mirrors as dictionary.
You have to play some tricks to it work...
def f(d):
from test import *
locals().update(d)
print a,b,c
d = dict(a=1, b=2, c=3)
f(d)
...and because you're not supposed to do this, you get a SyntaxWarning :-)
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, Aug 29 2012)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
2012-10-23: Python Meeting Duesseldorf ... 55 days to go
2012-08-28: Released mxODBC 3.2.0 ... http://egenix.com/go31
2012-08-20: Released mxODBC.Connect 2.0.0 ... http://egenix.com/go30
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
More information about the Python-ideas
mailing list