Questions tagged «subclass»

子类是从父(或超级)类派生或继承的类。子类在面向对象编程(OOP)中得到广泛使用。

1
Pandas DataFrame子类的属性设置器
我试图pd.DataFrame在初始化(group和timestamp_col)时建立一个具有两个必需参数的子类。我想对这些参数group和进行验证timestamp_col,所以我对每个属性都有一个setter方法。这一切都是有效的,直到我尝试set_index()得到TypeError: 'NoneType' object is not iterable。在test_set_index和中,似乎没有参数传递给我的setter函数test_assignment_with_indexed_obj。如果添加if g == None: return到setter函数中,则可以通过测试用例,但认为这不是正确的解决方案。 如何为这些必需的参数实施属性验证? 下面是我的课: import pandas as pd import numpy as np class HistDollarGains(pd.DataFrame): @property def _constructor(self): return HistDollarGains._internal_ctor _metadata = ["group", "timestamp_col", "_group", "_timestamp_col"] @classmethod def _internal_ctor(cls, *args, **kwargs): kwargs["group"] = None kwargs["timestamp_col"] = None return cls(*args, **kwargs) def __init__( self, …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.