我可以在 python 中有一个全局变量而不将其声明为全局变量吗?
can I have a global variable in python without declaring it as global?
我想要某种维护当前数字的全局状态,以及生成下一个数字的函数。
我可以写一个生成器来给我下一个数字。
def gen(self, n):
yield n
yield n + 1
但是保持其状态的干净方法是什么?我不想简单地拥有一个全局变量。有一个更好的方法吗?或者这是我唯一的选择?
我试着做了一个 class 这样的:
class Count:
"""
Represents the counter which
generates variables
"""
def __init__(self, curr=0):
"""
:param curr: the current integer
"""
self.curr = curr
def gen(self):
"""
A generator for the next
number
:return: generator
"""
self.curr += 1
yield self.curr
yield self.curr + 1
但这行不通,因为每次我创建 Count() 时,它都会重置我不想要的计数器。
如果我的理解是正确的,要消除 global
计数器,您可以为您的变量创建一个闭包,并 return 一个递增它的函数。
原函数counter
只被调用一次,连续调用只会增加计数器:
def count(n):
c = n
def incr():
nonlocal c
c += 1
print(c)
return c
return incr
count
初始化为一些状态 n
并且 incr
在连续调用中引用该状态:
>>> f = count(2)
>>> f()
3
>>> f()
4
>>> f()
5
或者你可以使用 next(generator) 函数。
def num_gen():
n=1
while n:
yield n
n += 1
然后
>>my_gen = num_gen()
>>next(my_gen)
1
>>next(my_gen)
2
等等。每当生成器产生一个值时,都会存储生成器的状态,以便稍后恢复执行。
如果您想跨 Count
的多个实例维护状态,请在 class 范围内使用一个变量,并使用 Count.
前缀引用它,如下所示:
class Count:
curr = 0
def __init__(self, startWith = None):
if startWith is not None: Count.curr = startWith - 1
def gen(self):
while True:
Count.curr += 1
yield Count.curr
请注意,如果您想保持状态,构造函数应该允许 不 重置计数器的可能性,但保持不变。
附带说明一下,您可能有兴趣让生成器生成如上所示的永无止境的系列。
下面是如何使用上面的 class:
# Get generator that starts with 2
gen = Count(2).gen();
print (next(gen)); # -> 2
print (next(gen)); # -> 3
print (next(gen)); # -> 4
# Get a generator that continues ...
gen2 = Count().gen();
print (next(gen2)); # -> 5
# We can still use the previous generator also:
print (next(gen)); # -> 6
# Start with 0:
gen3 = Count(0).gen();
print (next(gen3)); # -> 0
# Other generators follow suit:
print (next(gen)); # -> 1
print (next(gen2)); # -> 2
我想要某种维护当前数字的全局状态,以及生成下一个数字的函数。
我可以写一个生成器来给我下一个数字。
def gen(self, n):
yield n
yield n + 1
但是保持其状态的干净方法是什么?我不想简单地拥有一个全局变量。有一个更好的方法吗?或者这是我唯一的选择?
我试着做了一个 class 这样的:
class Count:
"""
Represents the counter which
generates variables
"""
def __init__(self, curr=0):
"""
:param curr: the current integer
"""
self.curr = curr
def gen(self):
"""
A generator for the next
number
:return: generator
"""
self.curr += 1
yield self.curr
yield self.curr + 1
但这行不通,因为每次我创建 Count() 时,它都会重置我不想要的计数器。
如果我的理解是正确的,要消除 global
计数器,您可以为您的变量创建一个闭包,并 return 一个递增它的函数。
原函数counter
只被调用一次,连续调用只会增加计数器:
def count(n):
c = n
def incr():
nonlocal c
c += 1
print(c)
return c
return incr
count
初始化为一些状态 n
并且 incr
在连续调用中引用该状态:
>>> f = count(2)
>>> f()
3
>>> f()
4
>>> f()
5
或者你可以使用 next(generator) 函数。
def num_gen():
n=1
while n:
yield n
n += 1
然后
>>my_gen = num_gen()
>>next(my_gen)
1
>>next(my_gen)
2
等等。每当生成器产生一个值时,都会存储生成器的状态,以便稍后恢复执行。
如果您想跨 Count
的多个实例维护状态,请在 class 范围内使用一个变量,并使用 Count.
前缀引用它,如下所示:
class Count:
curr = 0
def __init__(self, startWith = None):
if startWith is not None: Count.curr = startWith - 1
def gen(self):
while True:
Count.curr += 1
yield Count.curr
请注意,如果您想保持状态,构造函数应该允许 不 重置计数器的可能性,但保持不变。
附带说明一下,您可能有兴趣让生成器生成如上所示的永无止境的系列。
下面是如何使用上面的 class:
# Get generator that starts with 2
gen = Count(2).gen();
print (next(gen)); # -> 2
print (next(gen)); # -> 3
print (next(gen)); # -> 4
# Get a generator that continues ...
gen2 = Count().gen();
print (next(gen2)); # -> 5
# We can still use the previous generator also:
print (next(gen)); # -> 6
# Start with 0:
gen3 = Count(0).gen();
print (next(gen3)); # -> 0
# Other generators follow suit:
print (next(gen)); # -> 1
print (next(gen2)); # -> 2