gpt4 book ai didi

python - Python 类变量赋值的不规则性

转载 作者:行者123 更新时间:2023-11-28 17:22:27 25 4
gpt4 key购买 nike

我正在尝试创建一个具有多个类级变量的类,其中一些变量具有引用先前声明的类级变量的计算值。但是,我在某些点引用变量时遇到困难。

我的第一次尝试:

#!/usr/bin/env python
from decimal import Decimal
import math

class Foo(object):
NUM_BUCKETS = 10
BUCKET_SIZE = Decimal(1.0 / NUM_BUCKETS)
BUCKET_LABELS = tuple("BUCKET_{}".format(int(BUCKET_SIZE * i * 100)) for i in xrange(1, NUM_BUCKETS + 1))

print Foo.BUCKET_LABELS

结果:

> python test.py
Traceback (most recent call last):
File "test.py", line 5, in <module>
class Foo(object):
File "test.py", line 8, in Foo
BUCKET_LABELS = tuple("BUCKET_{}".format(int(BUCKET_SIZE * i * 100)) for i in xrange(1, NUM_BUCKETS + 1))
File "test.py", line 8, in <genexpr>
BUCKET_LABELS = tuple("BUCKET_{}".format(int(BUCKET_SIZE * i * 100)) for i in xrange(1, NUM_BUCKETS + 1))
NameError: global name 'BUCKET_SIZE' is not defined

尝试通过类名访问类变量也不起作用:

#!/usr/bin/env python
from decimal import Decimal
import math

class Foo(object):
NUM_BUCKETS = 10
BUCKET_SIZE = Decimal(1.0 / NUM_BUCKETS)
BUCKET_LABELS = tuple("BUCKET_{}".format(int(Foo.BUCKET_SIZE * i * 100)) for i in xrange(1, NUM_BUCKETS + 1))

print Foo.BUCKET_LABELS

结果:

> python test2.py
Traceback (most recent call last):
File "test2.py", line 5, in <module>
class Foo(object):
File "test2.py", line 8, in Foo
BUCKET_LABELS = tuple("BUCKET_{}".format(int(Foo.BUCKET_SIZE * i * 100)) for i in xrange(1, NUM_BUCKETS + 1))
File "test2.py", line 8, in <genexpr>
BUCKET_LABELS = tuple("BUCKET_{}".format(int(Foo.BUCKET_SIZE * i * 100)) for i in xrange(1, NUM_BUCKETS + 1))
NameError: global name 'Foo' is not defined

用硬编码值替换对 BUCKET_SIZE 的引用可以解决问题;即使在同一行中有另一个类级变量引用,它也能正常工作:

#!/usr/bin/env python
from decimal import Decimal
import math

class Foo(object):
NUM_BUCKETS = 10
BUCKET_SIZE = Decimal(1.0 / NUM_BUCKETS)
BUCKET_LABELS = tuple("BUCKET_{}".format(int(Decimal(0.1) * i * 100)) for i in xrange(1, NUM_BUCKETS + 1))

print Foo.BUCKET_LABELS

结果:

> python test3.py
('BUCKET_10', 'BUCKET_20', 'BUCKET_30', 'BUCKET_40', 'BUCKET_50', 'BUCKET_60', 'BUCKET_70', 'BUCKET_80', 'BUCKET_90', 'BUCKET_100')

有人知道在那个地方引用 BUCKET_SIZE 的正确方法吗?这是 Python 本身的错误吗? (顺便说一句,我正在运行 Python 2.7.5。)

最佳答案

首先,一个解决方案,只需编辑这一行(注意括号):

BUCKET_LABELS = tuple(["BUCKET_{}".format(int(BUCKET_SIZE * i * 100)) for i in xrange(1, NUM_BUCKETS + 1)]) 

现在,如果您对 为什么 它在 Python 中的工作方式以及为什么它不是错误感到好奇...好吧,这不是一件容易的事 :-) :

[i*2 for i in xrange(3)] 是一个列表理解。它生成一个实际列表,可以像这样使用,例如:

>>> a = [i*2 for i in xrange(3)]
>>> a
[0, 2, 4]

(i*2 for i in xrange(3)) 是一个生成器表达式。它的工作方式非常相似,但不完全相同,因为它不生成列表或元组,而是生成器:

>>> a = (i*2 for i in xrange(3))
>>> a
<generator object <genexpr> at 0x02CEE058>
>>> a.next()
0
>>> a.next()
2
>>> a.next()
4
>>> a.next()
Traceback (most recent call last):
File "<input>", line 1, in <module>
StopIteration
>>> a = (i*2 for i in xrange(3))
>>> tuple(a)
(0, 2, 4)
>>> tuple(a)
()

如果您好奇,可以在这里找到更多信息:generator expressions .

tl;dr 版本是不能直接访问生成器(你必须要求它生成它的内容,例如使用 next()),并且每个值只能生成一次(然后生成器移动到下一个,因此 next() 函数名称)。

所以,回到你的问题。在下面的表达式中,你实际上要求生成一个带有生成器表达式的元组,这本身就很好。尽管如此,您还是通过使用 Foo 类变量来完成此操作,这在生成器的情况下可能会出现问题。

BUCKET_LABELS = tuple("BUCKET_{}".format(int(BUCKET_SIZE * i * 100)) for i in xrange(1, NUM_BUCKETS + 1))

特别是,一旦您要求生成器从中生成元组,生成器实际上根本不知道 Foo.BUCKET_SIZE 变量(生成器在其范围内工作,与列表相反).这就是您收到此错误的原因。

因此,一种解决方案是简单地使用列表理解(无论如何更容易处理/直观)。

PS:Decimal() 函数可能并不像您想象的那样:

>>> NUM_BUCKETS = 10
>>> print Decimal(1.0 / NUM_BUCKETS)
0.1000000000000000055511151231257827021181583404541015625
>>> print round(1.0 / NUM_BUCKETS, 2)
0.1

PPS:xrange(1, NUM_BUCKETS + 1) 部分没有出现错误的原因,如果您对它感到好奇,是因为它在 生成器已构建,因此,该类变量实际上被替换为生成器的值...它不会提示它。

关于python - Python 类变量赋值的不规则性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40750409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com