默认参数是一个参数,如果在函数调用中未提供该参数的值,则该参数将采用默认值。以下示例给出了默认参数的想法,如果未通过默认参数,则会显示默认年龄-
#!/usr/bin/python # Function definition is here def printinfo( name, age = 35 ): "This prints a passed info into this function" print "Name: ", name print "Age ", age return; # Now you can call printinfo function printinfo( age=50, name="miki" ) printinfo( name="miki" )
输出结果
执行以上代码后,将产生以下结果-
Name: miki Age 50 Name: miki Age 35