Python 如何编写通用函数,根据传递的数据类型执行不同的操作

Python 如何编写通用函数,根据传递的数据类型执行不同的操作,python,pandas,generic-function,Python,Pandas,Generic Function,我正在尝试使用functools中的singledispatch编写一个通用函数。我希望函数根据传递的参数的类型表现出不同的行为-在本例中,它将是一列数据帧,可以是不同的数据类型:int64、float64、object、bool等 我试着做一些基本的实验: @singledispatch def sprint(data): print('default success') @sprint.register('float64') def _(data): print('floa

我正在尝试使用functools中的singledispatch编写一个通用函数。我希望函数根据传递的参数的类型表现出不同的行为-在本例中,它将是一列数据帧,可以是不同的数据类型:int64、float64、object、bool等

我试着做一些基本的实验:

@singledispatch
def sprint(data):
    print('default success')

@sprint.register('float64')
def _(data):
    print('float success')

@sprint.register('int64')
def _(data):
    print('int success')

# test
from sklearn.datasets import load_iris
data_i = load_iris()
df_iris = pd.DataFrame(data_i.data, columns=data_i.feature_names)

sprint(df_iris['sepal length (cm)'])
但很明显,我得到了一个错误,因为python没有查看列的dtype属性

有没有办法解决这个问题

我很感激你的帮助