在类定义中引用其他类属性的Dictionary类属性 [英] Dictionary class attribute that refers to other class attributes in the definition

查看:228
本文介绍了在类定义中引用其他类属性的Dictionary类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然有很多方法可以解决这个问题,但由于人格错误,我无法理解失败的性质。



尝试:

  class OurFavAnimals(object):
FAVE ='这是我们最喜欢的一个'
NOTFAVE ='NAH我们不关心一个'
UNKNOWN ='WHAT?'
FAVES = defaultdict(lambda:UNKNOWN,{x:FAVE for x in ['dog','cat']})
FAVES ['Crab'] = NOTFAVE

失败:

  3 NOTFAVE ='NAH WE DONT CARE FOR THE ONE'
4 UNKNOWN ='WHAT?'
----> 5 FAVES = defaultdict(lambda:UNKNOWN,{x:FAVE for x in ['dog','cat']})
6 FAVES ['Crab'] = NOTFAVE

NameError:全局名称FAVE未定义

为什么?为什么可以找到 UNKNOWN 而不是 FAVE ?是因为它在字典理解中?

解决方案

是的,这是因为它在字典理解中。请注意,它不是找到 UNKNOWN 它只是不寻找它,因为 UNKNOWN 仅在lambda中引用。如果您用其他任何东西来替代你的dict理解,以允许类定义成功,那么如果您尝试访问不存在的密钥(因为那将尝试调用该lambda),以后会收到错误。所以如果你把它更改为

  FAVES = defaultdict(lambda:UNKNOWN,{'a':1})

你会得到:

 >>>我们的FavAnimals.FAVES ['x'] 
追溯(最近的最后一次调用):
文件< pyshell#4>,第1行,< module>
OurFavAnimals.FAVES ['x']
文件< pyshell#2>,第5行,< lambda>
FAVES = defaultdict(lambda:UNKNOWN,{'a':1})
NameError:全局名称'UNKNOWN'未定义

在这两种情况下,原因是在类范围中定义的变量在嵌套作用域中不可用。换句话说,这是失败的原因:

  class Foo(object):
something =Hello
def meth(self):
print(something)

并且字典理解创建嵌套在类范围内的函数范围,因此他们不能直接访问类变量。另请参阅此相关问题


While there are numerous ways around this, because of a personality fault I can't let it go until I understand the nature of the failure.

Attempting:

class OurFavAnimals(object):
    FAVE = 'THATS ONE OF OUR FAVORITES'
    NOTFAVE = 'NAH WE DONT CARE FOR THAT ONE'
    UNKNOWN = 'WHAT?'
    FAVES = defaultdict(lambda: UNKNOWN, {x:FAVE for x in ['dog', 'cat']})
    FAVES['Crab'] = NOTFAVE 

Fails with:

      3     NOTFAVE = 'NAH WE DONT CARE FOR THAT ONE'
      4     UNKNOWN = 'WHAT?'
----> 5     FAVES = defaultdict(lambda: UNKNOWN, {x:FAVE for x in ['dog', 'cat']})
      6     FAVES['Crab'] = NOTFAVE

NameError: global name 'FAVE' is not defined

Why? Why can it find UNKNOWN but not FAVE? Is it because it's in a dictionary comprehension?

解决方案

Yes, it's because it's in a dictionary comprehension. Note that it's not "finding" UNKNOWN either; it's just not looking for it yet, because UNKNOWN is only referenced in a lambda. If you replace your dict comprehension with something else to allow the class definition to succeed, you'll get an error later if you try to access a nonexistent key (because then it will try to call that lambda). So if you change it to

FAVES = defaultdict(lambda: UNKNOWN, {'a': 1})

You'll get:

>>> OurFavAnimals.FAVES['x']
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    OurFavAnimals.FAVES['x']
  File "<pyshell#2>", line 5, in <lambda>
    FAVES = defaultdict(lambda: UNKNOWN, {'a': 1})
NameError: global name 'UNKNOWN' is not defined

In both cases, the reason is that variables defined in the class scope are not available in nested scopes. In other words, it's the same reason this fails:

class Foo(object):
    something = "Hello"
    def meth(self):
        print(something)

Both the lambda and the dictionary comprehension create function scopes that are nested in the class scope, so they don't have access to the class variables directly. See also this related question.

这篇关于在类定义中引用其他类属性的Dictionary类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆