compact()和extract()是PHP中的函数我发现非常方便.compact()获取符号表中的名称列表,并仅使用其值创建哈希表.提取物恰恰相反.例如,
$foo = 'what';
$bar = 'ever';
$a = compact('foo', 'bar');
$a['foo']
# what
$a['baz'] = 'another'
extract(a)
$baz
# another
Run Code Online (Sandbox Code Playgroud)
有没有办法在Python中做同样的事情?我环顾四周,最接近我的是这个线程,它似乎皱着眉头.
我知道locals(),globals()和vars(),但我怎样才能轻松选择其值的一个子集?
Python有没有更好的东西可以避免这种需要?