別のクラスを継承するサブクラスNewDataStructure
を定義しました。オブジェクト自体に作用するメソッドは、このサブクラスでうまく動作します。ただし、コピーを作成するメソッドは、サブクラスではなく親クラスのオブジェクトを返します。これは、他のメソッド内でそのメソッドを呼び出すときに、多くの問題を引き起こします。継承元の親クラスではなく、サブクラスが自身のコピーを返すようにするにはどうすればよいですか?
親クラスの名前付きメソッドがサブクラスのオブジェクトを返すように具体的に指示する方法はありますか?
継承されたすべてのメソッドが親クラスではなくサブクラスのオブジェクトを返すように指示する方法はありますか?
おそらく返されたオブジェクトを私のクラスの__init__
関数に渡すことができますか?それに応じて__init__
を修正する必要があります... Pythonicの方法は何ですか?
import pandas as pd
class NewDataStructure(pd.DataFrame):
def __init__(self, data, index, title):
super(NewDataStructure, self).__init__(data=data, index=index)
self.title = title
new_data_variable = NewDataStructure(data=None, index=None, title="")
changed = new_data_variable.unstack()
new_data_variable.reset_index(inplace=True)
unchanged = new_data_variable
print type(changed)
print type(unchanged)
<class 'pandas.core.series.Series'>
<class '__main__.NewDataStructure'>