python中的函数重载了吗?


问题内容

Python中是否可能有重载函数?在C#中,我会做类似的事情

void myfunction (int first, string second)
{
//some code
}
void myfunction (int first, string second , float third)
{
//some different code
}

然后当我调用该函数时,它将根据参数的数量在两者之间进行区分。是否可以在Python中做类似的事情?


问题答案:

编辑 有关Python
3.4中新的单调度通用函数,请参见http://www.python.org/dev/peps/pep-0443/

通常,您不需要在Python中重载函数。Python是动态类型的,并且支持函数的可选参数。

def myfunction(first, second, third = None):
    if third is None:
        #just use first and second
    else:
        #use all three

myfunction(1, 2) # third will be None, so enter the 'if' clause
myfunction(3, 4, 5) # third isn't None, it's 5, so enter the 'else' clause