我试图在另一个的定义中引用一个类成员,这似乎在列表理解中起作用,但不是dict或" tuple"理解:
class Bork(object):
DATA = (1, 2, 3)
LIST = [tuple(x * x for x in DATA) for z in (4, 5, 6)]
TUPLE = tuple(tuple(x * x for x in DATA) for z in (4, 5, 6)) # NameError: global name 'DATA' is not defined
DICT = {z: tuple(x * x for x in DATA) for z in (4, 5, 6)} # NameError: global name 'DATA' is not defined
DICT2 = dict((z, tuple(x * x for x in DATA)) for z in (4, 5, 6)) # NameError: global name 'DATA' is not defined
Bork()
我做的事情是愚蠢的,还是看起来不一致?如果这有所不同,这就是Python 2.7。