[Python-ideas] Unpack of sequences

Guido van Rossum guido at python.org
Wed Aug 29 21:12:33 CEST 2012


Also it won't work in Python 3.

On Wed, Aug 29, 2012 at 11:50 AM, M.-A. Lemburg <mal at egenix.com> wrote:
> 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/
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list