[英]What is the Python equivalent of static variables inside a function?
此 C/C++ 代码的惯用 Python 等价物是什么?
void foo()
{
static int counter = 0;
counter++;
printf("counter is %d\n", counter);
}
具体来说,如何在 function 级别而不是 class 级别实施 static 成员? 将 function 放入 class 会改变什么吗?
有点颠倒,但这应该有效:
def foo():
foo.counter += 1
print "Counter is %d" % foo.counter
foo.counter = 0
如果您希望计数器初始化代码位于顶部而不是底部,则可以创建一个装饰器:
def static_vars(**kwargs):
def decorate(func):
for k in kwargs:
setattr(func, k, kwargs[k])
return func
return decorate
然后使用这样的代码:
@static_vars(counter=0)
def foo():
foo.counter += 1
print "Counter is %d" % foo.counter
它仍然需要您使用foo.
前缀,不幸的是。
(信用: @ony )
您可以向函数添加属性,并将其用作静态变量。
def myfunc():
myfunc.counter += 1
print myfunc.counter
# attribute must be initialized
myfunc.counter = 0
或者,如果您不想在函数外设置变量,您可以使用hasattr()
来避免AttributeError
异常:
def myfunc():
if not hasattr(myfunc, "counter"):
myfunc.counter = 0 # it doesn't exist yet, so initialize it
myfunc.counter += 1
无论如何,静态变量是相当少见的,你应该为这个变量找到一个更好的地方,最有可能是在一个类中。
还可以考虑:
def foo():
try:
foo.counter += 1
except AttributeError:
foo.counter = 1
推理:
if
分支(想想StopIteration异常)许多人已经建议测试“hasattr”,但有一个更简单的答案:
def func():
func.counter = getattr(func, 'counter', 0) + 1
没有try/except,没有测试hasattr,只是getattr 使用默认值。
其他答案已经证明了您应该这样做的方式。 这里有一种你不应该的方式:
>>> def foo(counter=[0]):
... counter[0] += 1
... print("Counter is %i." % counter[0]);
...
>>> foo()
Counter is 1.
>>> foo()
Counter is 2.
>>>
默认值仅在第一次计算函数时初始化,而不是每次执行时初始化,因此您可以使用列表或任何其他可变对象来存储静态值。
这是一个完全封装的版本,不需要外部初始化调用:
def fn():
fn.counter=vars(fn).setdefault('counter',-1)
fn.counter+=1
print (fn.counter)
在 Python 中,函数是对象,我们可以通过特殊属性__dict__
简单地向它们添加或猴子补丁成员变量。 内置vars()
返回特殊属性__dict__
。
编辑:注意,与替代try:except AttributeError
答案不同,使用这种方法,变量将始终为初始化后的代码逻辑做好准备。 我认为try:except AttributeError
替代以下内容将不那么 DRY 和/或有尴尬的流程:
def Fibonacci(n):
if n<2: return n
Fibonacci.memo=vars(Fibonacci).setdefault('memo',{}) # use static variable to hold a results cache
return Fibonacci.memo.setdefault(n,Fibonacci(n-1)+Fibonacci(n-2)) # lookup result in cache, if not available then calculate and store it
EDIT2:当从多个位置调用函数时,我只推荐上述方法。 如果该函数仅在一处调用,则最好使用nonlocal
:
def TheOnlyPlaceStaticFunctionIsCalled():
memo={}
def Fibonacci(n):
nonlocal memo # required in Python3. Python2 can see memo
if n<2: return n
return memo.setdefault(n,Fibonacci(n-1)+Fibonacci(n-2))
...
print (Fibonacci(200))
...
Python 没有静态变量,但您可以通过定义一个可调用的类对象然后将其用作函数来伪造它。 另请参阅此答案。
class Foo(object):
# Class variable, shared by all instances of this class
counter = 0
def __call__(self):
Foo.counter += 1
print Foo.counter
# Create an object instance of class "Foo," called "foo"
foo = Foo()
# Make calls to the "__call__" method, via the object's name itself
foo() #prints 1
foo() #prints 2
foo() #prints 3
请注意, __call__
使类(对象)的实例可通过其自己的名称进行调用。 这就是上面调用foo()
调用类的__call__
方法的原因。 从文档:
通过在类中定义
__call__()
方法,可以使任意类的实例可调用。
使用生成器函数生成迭代器。
def foo_gen():
n = 0
while True:
n+=1
yield n
然后像这样使用它
foo = foo_gen().next
for i in range(0,10):
print foo()
如果你想要一个上限:
def foo_gen(limit=100000):
n = 0
while n < limit:
n+=1
yield n
如果迭代器终止(如上面的例子),你也可以直接循环遍历它,比如
for i in foo_gen(20):
print i
当然,在这些简单的情况下,最好使用 xrange :)
这是关于yield 语句的文档。
其他解决方案将计数器属性附加到函数,通常使用复杂的逻辑来处理初始化。 这不适用于新代码。
在 Python 3 中,正确的方法是使用nonlocal
语句:
counter = 0
def foo():
nonlocal counter
counter += 1
print(f'counter is {counter}')
有关nonlocal
语句的规范,请参阅PEP 3104 。
如果计数器是模块私有的,则应将其命名为_counter
。
使用函数的属性作为静态变量有一些潜在的缺点:
第二个问题的惯用 python 可能会用前导下划线命名变量,以表明它不打算被访问,同时在事后保持可访问。
另一种选择是使用词法闭包的模式,python 3 中的nonlocal
关键字支持这种模式。
def make_counter():
i = 0
def counter():
nonlocal i
i = i + 1
return i
return counter
counter = make_counter()
可悲的是,我不知道如何将此解决方案封装到装饰器中。
另一种选择可能是作为可变值容器的未记录参数。
def counter(*, _i=[0]):
_i[0] += 1
return _i[0]
这是有效的,因为默认参数是在定义函数时计算的,而不是在调用时计算的。
更清洁的可能是使用容器类型而不是列表,例如
def counter(*, _i = Mutable(0)):
_i.value += 1
return _i.value
但我不知道一个内置类型,它清楚地传达了目的。
_counter = 0 def foo(): global _counter _counter += 1 print 'counter is', _counter
Python 通常使用下划线来表示私有变量。 在 C 中在函数内部声明静态变量的唯一原因是将它隐藏在函数之外,这并不是真正惯用的 Python。
def staticvariables(**variables):
def decorate(function):
for variable in variables:
setattr(function, variable, variables[variable])
return function
return decorate
@staticvariables(counter=0, bar=1)
def foo():
print(foo.counter)
print(foo.bar)
就像上面 vincent 的代码一样,这将用作函数装饰器,并且必须以函数名作为前缀访问静态变量。 这段代码的优点(尽管无可否认,任何人都可能足够聪明来弄清楚)是您可以拥有多个静态变量并以更传统的方式初始化它们。
更具可读性,但更冗长(Python 之禅:显式优于隐式):
>>> def func(_static={'counter': 0}):
... _static['counter'] += 1
... print _static['counter']
...
>>> func()
1
>>> func()
2
>>>
有关其工作原理的说明,请参见此处。
在尝试了几种方法后,我最终使用了@warvariuc 答案的改进版本:
import types
def func(_static=types.SimpleNamespace(counter=0)):
_static.counter += 1
print(_static.counter)
Python 方法中的静态变量
class Count:
def foo(self):
try:
self.foo.__func__.counter += 1
except AttributeError:
self.foo.__func__.counter = 1
print self.foo.__func__.counter
m = Count()
m.foo() # 1
m.foo() # 2
m.foo() # 3
另一个(不推荐!)对可调用对象的扭曲,如https://stackoverflow.com/a/279598/916373 ,如果您不介意使用时髦的调用签名,则可以这样做
class foo(object):
counter = 0;
@staticmethod
def __call__():
foo.counter += 1
print "counter is %i" % foo.counter
>>> foo()()
counter is 1
>>> foo()()
counter is 2
溶液 n +=1
def foo():
foo.__dict__.setdefault('count', 0)
foo.count += 1
return foo.count
全局声明提供了此功能。 在下面的示例中(python 3.5 或更高版本使用“f”),计数器变量是在函数之外定义的。 在函数中将其定义为全局意味着函数外部的“全局”版本应该可用于该函数。 因此,每次函数运行时,它都会修改函数外的值,将其保留在函数之外。
counter = 0
def foo():
global counter
counter += 1
print("counter is {}".format(counter))
foo() #output: "counter is 1"
foo() #output: "counter is 2"
foo() #output: "counter is 3"
惯用的方法是使用一个类,它可以有属性。 如果您需要实例不分开,请使用单例。
有多种方法可以将“静态”变量伪造或混入 Python(目前未提及的一种方法是具有可变的默认参数),但这不是Pythonic 惯用的方法。 只需使用一个类。
或者可能是生成器,如果您的使用模式适合。
在这个问题的提示下,我可以提出另一种可能更好用的替代方案,并且对于方法和函数看起来都一样:
@static_var2('seed',0)
def funccounter(statics, add=1):
statics.seed += add
return statics.seed
print funccounter() #1
print funccounter(add=2) #3
print funccounter() #4
class ACircle(object):
@static_var2('seed',0)
def counter(statics, self, add=1):
statics.seed += add
return statics.seed
c = ACircle()
print c.counter() #1
print c.counter(add=2) #3
print c.counter() #4
d = ACircle()
print d.counter() #5
print d.counter(add=2) #7
print d.counter() #8
如果你喜欢这种用法,这里是实现:
class StaticMan(object):
def __init__(self):
self.__dict__['_d'] = {}
def __getattr__(self, name):
return self.__dict__['_d'][name]
def __getitem__(self, name):
return self.__dict__['_d'][name]
def __setattr__(self, name, val):
self.__dict__['_d'][name] = val
def __setitem__(self, name, val):
self.__dict__['_d'][name] = val
def static_var2(name, val):
def decorator(original):
if not hasattr(original, ':staticman'):
def wrapped(*args, **kwargs):
return original(getattr(wrapped, ':staticman'), *args, **kwargs)
setattr(wrapped, ':staticman', StaticMan())
f = wrapped
else:
f = original #already wrapped
getattr(f, ':staticman')[name] = val
return f
return decorator
您始终可以创建所谓的“函数对象”并为其提供标准(非静态)成员变量,而不是创建具有静态局部变量的函数。
既然你给出了一个用 C++ 编写的例子,我将首先解释什么是 C++ 中的“函数对象”。 “函数对象”只是具有重载operator()
任何类。 类的实例将表现得像函数。 例如,你可以写int x = square(5);
即使square
是一个对象(带有重载的operator()
)并且在技术上不是“函数”。 您可以为函数对象提供您可以为类对象提供的任何功能。
# C++ function object
class Foo_class {
private:
int counter;
public:
Foo_class() {
counter = 0;
}
void operator() () {
counter++;
printf("counter is %d\n", counter);
}
};
Foo_class foo;
在 Python 中,我们也可以重载operator()
只是该方法被命名为__call__
:
这是一个类定义:
class Foo_class:
def __init__(self): # __init__ is similair to a C++ class constructor
self.counter = 0
# self.counter is like a static member
# variable of a function named "foo"
def __call__(self): # overload operator()
self.counter += 1
print("counter is %d" % self.counter);
foo = Foo_class() # call the constructor
这是正在使用的类的示例:
from foo import foo
for i in range(0, 5):
foo() # function call
打印到控制台的输出是:
counter is 1
counter is 2
counter is 3
counter is 4
counter is 5
如果你想让你的函数接受输入参数,你也可以将它们添加到__call__
中:
# FILE: foo.py - - - - - - - - - - - - - - - - - - - - - - - - -
class Foo_class:
def __init__(self):
self.counter = 0
def __call__(self, x, y, z): # overload operator()
self.counter += 1
print("counter is %d" % self.counter);
print("x, y, z, are %d, %d, %d" % (x, y, z));
foo = Foo_class() # call the constructor
# FILE: main.py - - - - - - - - - - - - - - - - - - - - - - - - - - - -
from foo import foo
for i in range(0, 5):
foo(7, 8, 9) # function call
# Console Output - - - - - - - - - - - - - - - - - - - - - - - - - -
counter is 1
x, y, z, are 7, 8, 9
counter is 2
x, y, z, are 7, 8, 9
counter is 3
x, y, z, are 7, 8, 9
counter is 4
x, y, z, are 7, 8, 9
counter is 5
x, y, z, are 7, 8, 9
这个答案建立在@claudiu 的答案之上。
我发现每当我打算访问静态变量时,我总是不得不在函数名之前加上函数名,因此我的代码变得不那么清晰了。
也就是说,在我的函数代码中,我更愿意这样写:
print(statics.foo)
代替
print(my_function_name.foo)
所以,我的解决方案是:
statics
属性statics
作为my_function.statics
的别名from bunch import *
def static_vars(**kwargs):
def decorate(func):
statics = Bunch(**kwargs)
setattr(func, "statics", statics)
return func
return decorate
@static_vars(name = "Martin")
def my_function():
statics = my_function.statics
print("Hello, {0}".format(statics.name))
评论
我的方法使用了一个名为Bunch
的类,它是一个支持属性样式访问的字典,一种 JavaScript(参见关于它的原始文章,大约在 2000 年)
它可以通过pip install bunch
它也可以像这样手写:
class Bunch(dict):
def __init__(self, **kw):
dict.__init__(self,kw)
self.__dict__ = self
该答案表明setdefault不能真正满足OP如何创建静态局部变量的问题。
def fn():
fn.counter = vars(fn).setdefault('counter',-1)
它的作用时间只要fn。 在每个变量名之前添加前缀。 如果像这样删除它们:
def fn():
counter = vars(fn).setdefault('counter',-1)
counter += 1
print (counter)
没有错误,但counter始终为0,这告诉我vars(fn)不是访问局部变量,而是全局的,可能是装饰器或属性存储。
如果这项工作奏效,那将是我的首选解决方案。 但是,由于没有,我倾向于使用完全封装的类定义来创建此类静态var。
恕我直言,这是最简单的。 当然,这取决于您是否更熟悉功能和OOP编码样式。
我个人更喜欢以下装饰器。 各有各的。
def staticize(name, factory):
"""Makes a pseudo-static variable in calling function.
If name `name` exists in calling function, return it.
Otherwise, saves return value of `factory()` in
name `name` of calling function and return it.
:param name: name to use to store static object
in calling function
:type name: String
:param factory: used to initialize name `name`
in calling function
:type factory: function
:rtype: `type(factory())`
>>> def steveholt(z):
... a = staticize('a', list)
... a.append(z)
>>> steveholt.a
Traceback (most recent call last):
...
AttributeError: 'function' object has no attribute 'a'
>>> steveholt(1)
>>> steveholt.a
[1]
>>> steveholt('a')
>>> steveholt.a
[1, 'a']
>>> steveholt.a = []
>>> steveholt.a
[]
>>> steveholt('zzz')
>>> steveholt.a
['zzz']
"""
from inspect import stack
# get scope enclosing calling function
calling_fn_scope = stack()[2][0]
# get calling function
calling_fn_name = stack()[1][3]
calling_fn = calling_fn_scope.f_locals[calling_fn_name]
if not hasattr(calling_fn, name):
setattr(calling_fn, name, factory())
return getattr(calling_fn, name)
以下装饰器可用于创建静态函数变量。 它用自身的返回值替换声明的函数。 这意味着被装饰的函数必须返回一个函数。
def static_inner_self(func):
return func()
然后在一个函数上使用装饰器,该函数返回另一个带有捕获变量的函数:
@static_inner_self
def foo():
counter = 0
def foo():
nonlocal counter
counter += 1
print(f"counter is {counter}")
return foo
nonlocal
是必需的,否则 Python 认为counter
变量是局部变量而不是捕获的变量。 由于变量赋值counter += 1
,Python 的行为与此类似。 函数中的任何赋值都会使 Python 认为该变量是局部变量。
如果你不是在内部函数中赋值给变量,那么你可以忽略nonlocal
语句,例如,在这个函数中我用来缩进字符串的行,其中 Python 可以推断出变量是nonlocal
:
@static_inner_self
def indent_lines():
import re
re_start_line = re.compile(r'^', flags=re.MULTILINE)
def indent_lines(text, indent=2):
return re_start_line.sub(" "*indent, text)
return indent_lines
PS 有一个已删除的答案提出了相同的建议。 不知道作者为什么删了。 https://stackoverflow.com/a/23366737/195417
基于丹尼尔的回答(补充):
class Foo(object):
counter = 0
def __call__(self, inc_value=0):
Foo.counter += inc_value
return Foo.counter
foo = Foo()
def use_foo(x,y):
if(x==5):
foo(2)
elif(y==7):
foo(3)
if(foo() == 10):
print("yello")
use_foo(5,1)
use_foo(5,1)
use_foo(1,7)
use_foo(1,7)
use_foo(1,1)
我想添加这部分的原因是,静态变量不仅用于增加某个值,还用于检查静态变量是否等于某个值,作为现实生活中的例子。
静态变量仍然受到保护,仅在函数 use_foo() 的范围内使用
在此示例中,对 foo() 函数的调用与(相对于相应的 C++ 等效项)完全相同:
stat_c +=9; // in c++
foo(9) #python equiv
if(stat_c==10){ //do something} // c++
if(foo() == 10): # python equiv
#add code here # python equiv
Output :
yello
yello
如果类 Foo 被限制性地定义为单例类,那将是理想的。 这将使它更加pythonic。
但是,这是一个安静的旧帖子,因为我有一个不同的惯用目标,所以我提出以下内容:
在一个函数中,我只想通过一个计算值初始化一次变量,这可能会有点昂贵。
因为我喜欢出色的写作,并且是一名老的C风格程序员。 我试图定义类似宏的文字:
def Foo () :
StaticVar( Foo, ‘Var’, CalculateStatic())
StaticVar( Foo, ‘Step’, CalculateStep())
Foo.Var += Foo.Step
print(‘Value of Var : ‘, Foo.Var)
然后,我这样写了“ StaticVar”:
def StaticVar(Cls, Var, StaticVal) :
if not hasattr(Cls, Var) :
setattr(Cls, Var, StaticVal)
在Python中甚至更好:
def StaticVars(Cls, **Vars) :
for Var, StaticVal in Vars.items() :
if not hasattr(Cls, Var) :
setattr(Cls, Var, StaticVal)
def Foo () :
StaticVars( Foo, Var = CalculateStatic(),Step= CalculateStep()))
Foo.Var += Foo. Step
print(‘Value of Var : ‘, Foo.Var)
这是一种不错的书写方式,但是我的目标(仅一次调用初始化函数)没有达到(只需在初始化函数中添加打印件)! 事实是,在函数调用中,参数值是在调用函数之前进行求值的。
def CalculateStatic() :
print("Costly Initialization")
return 0
为了实现我的目标,我宁愿写:
def Foo () :
if not hasattr(Foo, ‘Var’) :
setattr ( Foo, ‘Var’, CalculateStatic())
setattr ( Foo, ‘Step’, CalculateStep())
Foo.Var += Foo. Step
print(‘Value of Var : ‘, Foo.Var)
如果Python具有“ Marcro预处理”功能,那就更好了。
我写了一个简单的函数来使用静态变量:
def Static():
### get the func object by which Static() is called.
from inspect import currentframe, getframeinfo
caller = currentframe().f_back
func_name = getframeinfo(caller)[2]
# print(func_name)
caller = caller.f_back
func = caller.f_locals.get(
func_name, caller.f_globals.get(
func_name
)
)
class StaticVars:
def has(self, varName):
return hasattr(self, varName)
def declare(self, varName, value):
if not self.has(varName):
setattr(self, varName, value)
if hasattr(func, "staticVars"):
return func.staticVars
else:
# add an attribute to func
func.staticVars = StaticVars()
return func.staticVars
如何使用:
def myfunc(arg):
if Static().has('test1'):
Static().test += 1
else:
Static().test = 1
print(Static().test)
# declare() only takes effect in the first time for each static variable.
Static().declare('test2', 1)
print(Static().test2)
Static().test2 += 1
Miguel Angelo 的自我重定义解决方案甚至可以在没有任何装饰器的情况下实现:
def fun(increment=1):
global fun
counter = 0
def fun(increment=1):
nonlocal counter
counter += increment
print(counter)
fun(increment)
fun() #=> 1
fun() #=> 2
fun(10) #=> 12
第二行必须进行调整才能获得有限的 scope:
def outerfun():
def innerfun(increment=1):
nonlocal innerfun
counter = 0
def innerfun(increment=1):
nonlocal counter
counter += increment
print(counter)
innerfun(increment)
innerfun() #=> 1
innerfun() #=> 2
innerfun(10) #=> 12
outerfun()
装饰器的优点是您不必额外注意构造的 scope。
当然这是一个老问题,但我想我可能会提供一些更新。
性能参数似乎已经过时了。 相同的测试套件似乎为 siInt_try 和 isInt_re2 提供了相似的结果。 当然,结果各不相同,但这是我的计算机上的一个会话,在内核 4.3.01 和 Xeon W3550 上使用 python 3.4.4。 我已经运行了几次,结果似乎很相似。 我将全局正则表达式移动到静态函数中,但性能差异可以忽略不计。
isInt_try: 0.3690
isInt_str: 0.3981
isInt_re: 0.5870
isInt_re2: 0.3632
随着性能问题的解决,似乎 try/catch 会产生最适合未来和角落案例的代码,所以也许只需将它包装在函数中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.