由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
_Python版 - Static variables in function
相关主题
how to save a path in a string variable关于得到generic type的问题求助! (转载)
Static variables in function[转载] 先进看看这个UNIX C程序
[合集] C问题求助:如何强行从外部访问local static variable?关于得到generic type的问题求助!
请教一个const和non const的C++问题[合集] 为什么不能: declare a static memeber func
C/C++ Questions一个简单的算法问题?
python下的expectjava producer consumer problem (转载)
问一个函数指针的问题,c++a C question about global variable
C 和 C++ 相比有什么优点?bloomberg电面面经
相关话题的讨论汇总
话题: static话题: var话题: func话题: function话题: def
1 (共1页)
i*****f
发帖数: 578
1
# method 1: a callable instance
# pros: good encapsulation
# cons: too verbal
class _func_with_static_var:
def __init__(self):
self.static_var = 0
def __call__(self, *args):
self.static_var += 1
print self.static_var
func_with_static_var = _func_with_static_var()
# method 2: pass as a list with default value
# pros: less verbal
# cons: affect the function interface
def func_with_static_var( arg1, arg2, static_var = [0] ):
static_var[0] += 1
print static_var[0]
# met
b****j
发帖数: 78
2
function itself is object, so:
def f():
if not hasattr(f, 'a'):
f.a = 0
f.a += 1
print f.a

【在 i*****f 的大作中提到】
: # method 1: a callable instance
: # pros: good encapsulation
: # cons: too verbal
: class _func_with_static_var:
: def __init__(self):
: self.static_var = 0
: def __call__(self, *args):
: self.static_var += 1
: print self.static_var
: func_with_static_var = _func_with_static_var()

i*****f
发帖数: 578
3
oh, that's a nice one!

【在 b****j 的大作中提到】
: function itself is object, so:
: def f():
: if not hasattr(f, 'a'):
: f.a = 0
: f.a += 1
: print f.a

1 (共1页)
相关主题
bloomberg电面面经C/C++ Questions
请教cosnt的使用python下的expect
奇怪的 printf!! ksh programming问一个函数指针的问题,c++
郁闷死了,请教一个R的问题C 和 C++ 相比有什么优点?
how to save a path in a string variable关于得到generic type的问题求助! (转载)
Static variables in function[转载] 先进看看这个UNIX C程序
[合集] C问题求助:如何强行从外部访问local static variable?关于得到generic type的问题求助!
请教一个const和non const的C++问题[合集] 为什么不能: declare a static memeber func
相关话题的讨论汇总
话题: static话题: var话题: func话题: function话题: def