Pythonic方式做静态局部变量? [英] Pythonic way to do static local variables?
问题描述
我的函数需要在
调用之间维持一个有序的序列。
在C或C ++中,我会声明指针(或集合对象)静态在
函数范围。
Pythonic的方法是什么?
>
有没有比将序列放在模块范围更好的解决方案?
谢谢。
>我有一个函数需要在调用之间维护一个有序的序列。
在C或C ++中,我将声明指针(或集合对象)静态在函数范围内。
Pythonic的方法是什么?
有没有比将序列放在模块范围更好的解决方案? / blockquote>
是的,有;它被称为面向对象的编程。一般来说,每当你发现一个或多个函数必须访问上下文时,
没有明确地作为参数传递给你的
,正确的(pythonic和
非pythonic)方法是将它们定义为类中存储
此上下文的方法。
class Foo(object):
def __init __(自我):
self._context =#something
def方法(个体经营):
x = self._context
#do stuff
self._context.update()
另一个解决方案是函数/方法属性。如果OO
对于单个函数来说似乎有点过分,或者在方法的情况下,如果你不想使用某些东西来污染实例命名空间,这是很方便的通过
单一方法:
def foo():
#foo.x就像一个C静态
try:foo.x + = 1
除了AttributeError:
foo.x = 1
return foo.x
$ x $ b for x in xrange(10):print foo()
如果foo()是一种方法,它会变得有点麻烦,第一个
,因为你必须引用类名和(不太明显)
因为实例方法中不允许用户定义的属性(I
奇怪为什么);你必须参考包装的功能:
class Foo(对象):
def foo(self):
试试:Foo.foo.im_func.x + = 1
除了AttributeError:
Foo.foo.im_func.x = 1
返回Foo。 foo.x
foo = Foo()。foo
$ x $ b for i in xrange(10):print foo()
希望这会有所帮助,
George
好吧,如果你是c ++程序员,那么你'曾经有可能在某个时间遇到了b $ b'仿函数'。您可以通过制作一个可调用的
python对象来模拟它。
类仿函数:
def __init __(自我):
self.ordered_sequence = [1,2,3,4,5]
def __call __(self,arg1,arg2):
self.ordered_sequence.extend((arg1,arg2))
self.ordered_sequence.sort()
< blockquote class =post_quotes> f = functor()
f(3,5)
f.ordered_sequence
[1,2,3,3, 4,5,5]
希望有所帮助。
jw
于4/25/05 ,Charles Krug< cd **** @ worldnet.att.net>写道:我有一个函数,需要在
调用之间维护一个有序的序列。
在C或C ++中,我将声明指针(或集合对象)静态在<功能范围。
Pythonic的方法是什么?
有没有比将序列放在模块范围更好的解决方案?/ >
谢谢。
http://mail.python.org/mailman/listinfo/python-list
Charles Krug写道:< blockquote class =post_quotes>我有一个需要在
调用之间维持有序序列的函数。
在C或C ++中,我会声明指针(或集合)在函数范围内是静态的。
什么是Pythonic的方法呢?
有没有比将序列放在模块上更好的解决方案范围?
谢谢。
你可能想要一台发电机。搜索yield关键字。
问候,
Nicolas
I''ve a function that needs to maintain an ordered sequence between
calls.
In C or C++, I''d declare the pointer (or collection object) static at
the function scope.
What''s the Pythonic way to do this?
Is there a better solution than putting the sequence at module scope?
Thanks.
> I''ve a function that needs to maintain an ordered sequence betweencalls.
In C or C++, I''d declare the pointer (or collection object) static at
the function scope.
What''s the Pythonic way to do this?
Is there a better solution than putting the sequence at module scope?
Yes, there is; it''s called "object oriented programming". In general,
whenever you find that one or more functions have to access a context
that is not passed in explicitly as arguments, the proper (pythonic and
non-pythonic) way is to define them as a method in a class that stores
this context.
class Foo(object):
def __init__(self):
self._context = # something
def method(self):
x = self._context
# do stuff
self._context.update()
Another solution is function/method attributes. This is handy if OO
seems an overkill for a single function, or in case of methods, if you
don''t want to pollute the instance namespace with something used by a
single method:
def foo():
# foo.x is like a C static
try: foo.x +=1
except AttributeError:
foo.x = 1
return foo.x
for i in xrange(10): print foo()
if foo() is a method, it becomes a little more cumbersome, first
because you have to refer to the class name and (less obviously)
because user defined attributes are not allowed in instance methods (I
wonder why); you have to refer to the wrapped function:
class Foo(object):
def foo(self):
try: Foo.foo.im_func.x += 1
except AttributeError:
Foo.foo.im_func.x = 1
return Foo.foo.x
foo = Foo().foo
for i in xrange(10): print foo()
Hope this helps,
George
Well, if you''re a c++ programmer, then you''ve probably ran into
`functors'' at one time or another. You can emulate it by making a
python object that is `callable''.
class functor:
def __init__(self):
self.ordered_sequence = [1, 2, 3, 4, 5]
def __call__(self, arg1, arg2):
self.ordered_sequence.extend((arg1,arg2))
self.ordered_sequence.sort()
f = functor()
f(3,5)
f.ordered_sequence[1, 2, 3, 3, 4, 5, 5]
Hope that helps some.
jw
On 4/25/05, Charles Krug <cd****@worldnet.att.net> wrote: I''ve a function that needs to maintain an ordered sequence between
calls.
In C or C++, I''d declare the pointer (or collection object) static at
the function scope.
What''s the Pythonic way to do this?
Is there a better solution than putting the sequence at module scope?
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
Charles Krug wrote:I''ve a function that needs to maintain an ordered sequence between
calls.
In C or C++, I''d declare the pointer (or collection object) static at
the function scope.
What''s the Pythonic way to do this?
Is there a better solution than putting the sequence at module scope?
Thanks.
You might want a generator. Search for yield keyword.
Regards,
Nicolas
这篇关于Pythonic方式做静态局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!