Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python数据类时发生TypeError_Python_Python Dataclasses - Fatal编程技术网

使用python数据类时发生TypeError

使用python数据类时发生TypeError,python,python-dataclasses,Python,Python Dataclasses,在上面的代码中,我为行索引得到了这个错误:List[str]=[vi]+bands: ***TypeError:只能将列表(而不是“字段”)连接到列表 如何解决此问题?您可以在\uuu post\uu init\uuu中定义索引。它不会出现在repr中,但可以作为属性访问 您还需要为default\u factory调用一个可调用的,因此列表而不是[] 下面是一个简化的示例(因为我不知道什么是设置: @dataclass class cntr(setup): source:str = '

在上面的代码中,我为行
索引得到了这个错误:List[str]=[vi]+bands

***TypeError:只能将列表(而不是“字段”)连接到列表


如何解决此问题?

您可以在
\uuu post\uu init\uuu
中定义
索引。它不会出现在
repr
中,但可以作为属性访问

您还需要为
default\u factory
调用一个可调用的,因此
列表
而不是
[]

下面是一个简化的示例(因为我不知道什么是
设置

@dataclass
class cntr(setup):
    source:str = 'S2'
    vi:str = 'SW'
    # Dataframe containing information on samples
    df:pd.DataFrame = pd.DataFrame()

    # Available bands
    bands:List[str] = field(default_factory=[])

    indices:List[str] = [vi] + bands
@dataclass
class cntr():
    source:str = 'S2'
    vi:str = 'SW'
    # Available bands
    bands:List[str] = field(default_factory=list)

    def __post_init__(self):
        self.indices:List[str] = [self.vi] + self.bands
c = cntr()
c.indices  # will print: ['SW']