代码之家  ›  专栏  ›  技术社区  ›  Jacques de Hooge

数据类行为的变化

  •  3
  • Jacques de Hooge  · 技术社区  · 6 年前

    我正在为的3.7.1版本工作 Transcrypt Python到JavaScript编译器。

    它以前在cpython3.7的beta版上运行得很完美,但是在发布版上有一个问题。

    from dataclasses import dataclass
    from typing import ClassVar
    
    @dataclass
    class Test:
        x: ClassVar = 10
        y:  int = 10
    
    t = Test ()
    
    t.x = 20
    
    print (repr (t))
    

    用于打印(使用CPython):

    Test(x=20, y=10)
    

    Test(y=10)
    

    有谁能告诉我这个改变是不是有意的,

    在这种变化下,Transcrypt的行为与CPython不同,这是我不想要的。 所以我想知道我应该采用Transcrypt还是应该把它看作是CPython回归并等待它被解决。

    1 回复  |  直到 6 年前
        1
  •  5
  •   Chris    6 年前

    the documentation :

    dataclass() 实际检查a的类型 PEP 526 typing.ClassVar . 如果字段是 ClassVar ,它被排除在 作为一个字段,被数据类机制忽略。 类别变量 fields()

    所以,这似乎是有意的改变。