[英]TypeError during using python dataclass
@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
In the code above, I get this error for the line indices:List[str] = [vi] + bands
:在上面的代码中,我收到以下行
indices:List[str] = [vi] + bands
错误indices:List[str] = [vi] + bands
:
*** TypeError: can only concatenate list (not "Field") to list
How do I fix this?我该如何解决?
You can define indices
in __post_init__
.您可以在
__post_init__
定义indices
。 It will not appear in the repr
but it will be accessible as a property.它不会出现在
repr
但可以作为属性访问。
You also need to have a callable for default_factory
, so list
instead of []
.您还需要有一个可调用的
default_factory
,所以list
而不是[]
。
Here is a simplified example (as I do not know what is setup
:这是一个简化的示例(因为我不知道是什么
setup
:
@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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.