Python相当于PHP的compact()和extract() [英] Python equivalent of PHP's compact() and extract()

查看:131
本文介绍了Python相当于PHP的compact()和extract()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

compact ()和 extract ()是PHP中的函数,我发现非常方便。 compact()获取符号表中的名称列表,并使用其值创建散列表。提取相反。例如,

  $ foo ='what'; 
$ bar ='ever';
$ a = compact('foo','bar');
$ a ['foo']
#what
$ a ['baz'] ='another'
extract(a)
$ baz
#另一个

有没有办法在Python中做同样的事情?我看了四周,最近我来的是这个线程,似乎皱眉



我知道locals(),globals()和vars(),但是如何轻松地只选择一个子集的值?



Python是否有更好的东西可以避免这种需求?

解决方案

这不是很Pythonic,但如果你真的必须:

  import inspect 

def compact(* names):
caller = inspect.stack()[1] [0]#compact()的调用者
vars = {}
在名称中为n:
如果在caller.f_locals中为n:
vars [n] = caller.f_locals [n]
elif n在caller.f_globals中:
vars [n] = caller.f_globals [n]
return vars

def extract(vars):
caller = inspect.stack()[1] [0]#调用者extract()
为n,v在vars.items()中:
caller.f_locals [n] = v#永远不要这样 - 不保证工作

我已经使用这些实现相当多,而且它们工作,但技术上修改 f_locals 不支持。



认真地说,如果你真的觉得你需要使用这些功能,你可能在做错事了。似乎对 Python的理念至少有三个计数:显而易见比隐含的更好,简单比复杂,如果实现很难解释,这是一个坏主意,也许更多(真的,如果你有足够的Python经验,你知道这样的东西只是不做)我可以看到它对于调试器或验证后分析是有用的,或者可能是某些通常需要使用动态选择的名称和值创建变量的非常通用的框架,但这是一个延伸。 / p>

如果你要使用这些函数,你应该至少保留包含在小范围内的提取 。将它们包装在你可以认为是黑盒子的功能中。 extract 的主要原因是它将变量放在符号表中,方法是检查代码不清楚。如果您将这些变量的效果保留在非常小的函数中,并通过清除代码和注释来解释您正在做什么,那么这不是一个大问题。


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. extract does the opposite. e.g.,

$foo = 'what';
$bar = 'ever';
$a = compact('foo', 'bar');
$a['foo']
# what
$a['baz'] = 'another'
extract(a)
$baz
# another

Is there a way to do the same in Python? I've looked all around and the closest I've come is this thread, which seems to frown on it.

I know about locals(), globals() and vars(), but how can I handily select just a subset of their values?

Does Python have something even better that obviates the need for this?

解决方案

It's not very Pythonic, but if you really must:

import inspect

def compact(*names):
    caller = inspect.stack()[1][0] # caller of compact()
    vars = {}
    for n in names:
        if n in caller.f_locals:
            vars[n] = caller.f_locals[n]
        elif n in caller.f_globals:
            vars[n] = caller.f_globals[n]
    return vars

def extract(vars):
    caller = inspect.stack()[1][0] # caller of extract()
    for n, v in vars.items():
        caller.f_locals[n] = v   # NEVER DO THIS - not guaranteed to work

I've used these implementations quite a bit, and they work, but technically modifying f_locals is not supported.

Seriously though, if you really feel you have a need to use these functions, you're probably doing something the wrong way. It seems to run against Python's philosophy on at least three counts: "explicit is better than implicit", "simple is better than complex", "if the implementation is hard to explain, it's a bad idea", maybe more (and really, if you have enough experience in Python you know that stuff like this just isn't done). I could see it being useful for a debugger or post-mortem analysis, or perhaps for some sort of very general framework that frequently needs to create variables with dynamically chosen names and values, but it's a stretch.

If you are going to use these functions, you should at least keep the extracted variables contained to within small scopes. Wrap them in functions that you can then consider to be "black boxes". The main reason extract is bad is that it puts variables in your symbol table in a way that isn't clear from inspecting the code. If you keep the effects of those variables localized to a very small function, and explain what you're doing with clear code and comments, it's not that big of a problem.

这篇关于Python相当于PHP的compact()和extract()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆