Python 3 - 未定义类变量

ram*_*esh 8 python scope python-3.x

在这里,我无法访问Python 列表推导中的类变量 .

class Student:
  max_year = 18
  year_choice = [i for i in range(100) if i > max_year]

  def __init__(self, name):
    self.name = name
    print (self.year_choice)

Student('Blah')
Run Code Online (Sandbox Code Playgroud)

但它在Python 2中运行良好.

../workspace$ python student.py
[19, 20, 21, 22, 2.... 99]
Run Code Online (Sandbox Code Playgroud)

但是在Python 3中出现错误.

../workspace$ python student.py
File "student.py", line 18, in <listcomp>
year_choice = [i for i in range(100) if i > max_year]
NameError: name 'max_year' is not defined
Run Code Online (Sandbox Code Playgroud)

从调试这个当我改变下面的声明

[i for i in range(100) if i > max_year]

对此

[i for i in range(max_year)] # don't look too much into it ;)
Run Code Online (Sandbox Code Playgroud)

工作正常.为什么我无法访问if/else list comprehension中的类变量?

PM *_*ing 5

这条线的原因

year_choice = [i for i in range(100) if i > max_year]
Run Code Online (Sandbox Code Playgroud)

在Python 2中有效,但在Python 3中无效,原因在于在Python 3中,列表推导创建了一个新范围,而max_yearclass属性不在该范围内。在Python 2中,列表推导不会创建新的作用域,而是在周围代码的上下文中运行。这样做最初是出于性能方面的考虑,但很多人发现它令人困惑,因此在Python 3中对其进行了更改,使列表理解与生成器表达式一致,并对set和dict进行了理解。

AFAIK,Python 3中没有简单的方法来访问在列表的理解内运行的类属性,该列表理解在类的外部上下文中而不是在方法内部运行。您不能使用引用它Student.max_year,因为那时Student该类不存在。

但是,无论如何,对列表的理解确实没有意义。您可以更紧凑,更高效地创建所需的列表。例如:

class Student(object):
    max_year = 18
    year_choice = list(range(max_year + 1, 100))

    def __init__(self, name):
        self.name = name
        print (self.year_choice)

Student('Blah')
Run Code Online (Sandbox Code Playgroud)

输出

[19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
Run Code Online (Sandbox Code Playgroud)

该代码在Python 2和Python 3上产生相同的输出。

我将班级签名更改为

class Student(object):
Run Code Online (Sandbox Code Playgroud)

以便在Python 2中创建一个新样式的类(在Python 3中所有类都是新样式)。


[i for i in range(max_year)]可以克服此限制的原因是,创建了一个迭代器range(max_year),然后将其作为参数传递给运行列表推导的临时函数。因此,它等效于以下代码:

class Student(object):
    max_year = 18
    def _listcomp(iterator):
        result = []
        for i in iterator:
            result.append(i)
        return result

    year_choice = _listcomp(iter(range(max_year + 1, 100)))
    del _listcomp

    def __init__(self, name):
        self.name = name
        print(self.year_choice)

Student('Blah')
Run Code Online (Sandbox Code Playgroud)

非常感谢Antti Haapala的解释。