Python equivalent of PHP's compact() and extract()

后端 未结 6 969
误落风尘
误落风尘 2020-11-28 11:22

compact() and extract() are functions in PHP I find tremendously handy. compact() takes a list of names in the symbol table and creates a hashtable with just their values.

6条回答
  •  -上瘾入骨i
    2020-11-28 11:59

    You can do this (though I would suggest only sparingly...) with the locals() function, which returns an updatable dict. Example:

    $ python
    Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
    >>> locals().update({'derek':'anderson'})
    >>> derek
    'anderson'
    >>> 
    

    So locals() would be "compact all" and locals().update() would be extract.

    Best of luck!

提交回复
热议问题