简体   繁体   English

如何在python中定义临时变量?

[英]How to define a temporary variable in python?

Does python have a "temporary" or "very local" variable facility? python是否具有“临时”或“非常本地”变量工具? I am looking for a one-liner and I want to keep my variable space tidy. 我正在寻找一个单行,我想保持我的变量空间整洁。

I would like to do something like this: 我想做这样的事情:

...a, b, and c populated as lists earlier in code...
using ix=getindex(): print(a[ix],b[ix],c[ix])
...now ix is no longer defined...

The variable ix would be undefined outside of the one line. 变量ix将在一行之外未定义。

Perhaps this pseudo-code is more clear: 也许这个伪代码更清楚:

...a and b are populated lists earlier in code...
{ix=getindex(); answer = f(a[ix]) + g(b[ix])}

where ix does not exist outside of the bracket. 其中ix不存在于括号外。

Comprehensions and generator expressions have their own scope, so you can put it in one of those: 理解和生成器表达式有自己的范围,因此您可以将其放在其中一个:

>>> def getindex():
...     return 1
...
>>> a,b,c = range(2), range(3,5), 'abc'
>>> next(print(a[x], b[x], c[x]) for x in [getindex()])
1 4 b
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

But you really don't have to worry about that sort of thing. 但你真的不必担心那种事情。 That's one of Python's selling points. 这是Python的卖点之一。

For those using Python 2: 对于那些使用Python 2的人:

>>> print next(' '.join(map(str, [a[x], b[x], c[x]])) for x in [getindex()])
1 4 b

Consider using Python 3, so you don't have to deal with print as a statement. 考虑使用Python 3,因此您不必将print作为语句处理。

Technically not an answer, but: Don't worry about temporary variables too much, they are only valid within their local scope (most likely function in your case) and garbage collector gets rid of them as soon as that function is finished. 从技术上讲,这不是一个答案,但是:不要过多担心临时变量,它们只在本地范围内有效(在您的情况下很可能是函数),垃圾收集器在该函数完成后立即删除它们。 One liners in python are mostly used for dict and list comprehensions. python中的一个衬里主要用于字典和列表推导。

If you REALLY want to do it in one line, use lambda which is pretty much keyword for inline function 如果您真的想在一行中执行它,请使用lambda ,它几乎是内联函数的关键字

Does python have a "temporary" or "very local" variable facility? python是否具有“临时”或“非常本地”变量工具?

yes, it's called a block, eg a function: 是的,它被称为一个块,例如一个函数:

def foo(*args):
    bar = 'some value' # only visible within foo
    print bar # works
foo()
> some value
print bar # does not work, bar is not in the module's scope
> NameError: name 'bar' is not defined

Note that any value is temporary in the sense that it is only guaranteed to stay allocated as long as a name is bound to it. 请注意,任何值都是临时的,只要名称绑定到它,它就只能保证保持分配状态。 You can unbind by calling del : 你可以通过调用del来取消绑定:

bar = 'foo'
print bar # works
> foo
del bar
print bar # fails
> NameError: name 'bar' is not defined

Note that this does not directly deallocate the string object that is 'foo' . 请注意,这不会直接释放'foo'的字符串对象。 That's the job of Python's garbage collector which will clean up after you. 这是Python的垃圾收集器的工作,它将在你之后清理。 In almost all circumstances there is however no need to deal with unbinding or the gc explicitely. 在几乎所有情况下,都没有必要明确地处理解除绑定或gc。 Just use variables and enjoy the Python livestyle. 只需使用变量并享受Python livestyle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM