【再回首Python之美】【函数】自定义函数

自定义函数:

    使用自定义函数可以提高公共功能代码的使用率,减少工程代码总体代码量。

    当出现问题时,可以做到修改一个函数而一劳永逸。

示例代码:

#ex_function.py
self_file = __file__    #save current file absolute path

print "\ndefine myself function without param"
def hello_without_param():
    print "hello world"
hello_without_param()       #hello world.

print "\ndefine myself function with param"
def hello_with_param(somebody):
    print "hello %s" % somebody
hello_with_param("Tom")     #hello Tom.

print "\ndefine myself function with default param"
def hello_with_defaultparam(somebody = "Lucy"):
    print "hello %s" % somebody
hello_with_defaultparam()           #hello Lucy
hello_with_defaultparam("Kitty")    #hello Kitty

print "\ndefine myself function with many default param"
#部分入参提供默认参数时,这些参数必须在参数列表最后
def hello_withmany_defaultparam(op, somebody = "Lucy"):
    print "%s %s" % (op, somebody)
hello_withmany_defaultparam("hello")            #hello Lucy
hello_withmany_defaultparam("hello", "Kitty")   #hello Kitty
hello_withmany_defaultparam("Like", "Kitty")    #Like Kitty

print "\n exit %s" % self_file

编译执行:


(end)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值