向python类添加用户定义的方法。
f2m的Python项目详细描述
将用户定义函数转换为方法
熊猫数据帧示例
importpandasaspdfromf2mimportf2m# create a new class that inherits from pd.DataFrame# and includes methods defined in a 'helper.py' fileF2mFrame=f2m(cls=pd.DataFrame,src='helper')# instantiate the new classdf=F2mFrame(data=pd.read_csv('risk_factors_cervical_cancer.csv'))# test methods added from helper filedf.say_hi()df.say_moo()# test method from parent classdf.head(n=1)# confirm that df is an instance of pd.DataFrame and PydyFrameisinstance(df,(pd.DataFrame,F2mFrame))# confirm that F2mFrame is a subclass of pd.DataFrameissubclass(F2mFrame,pd.DataFrame)