鱼C论坛

 找回密码
 立即注册
查看: 2502|回复: 0

[技术交流] python入门L18

[复制链接]
发表于 2017-6-26 03:56:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
函数的参数:
形参和实参
形参是定义函数时小括号里面的参数
Def myfirstfunction(name):
        '函数定义过程中的name是叫形参'
        #因为ta只是一个形式,表示占据一个参数位置
        Print('传递进来的'+name+‘叫做实参,因为ta时具体的参数值!’)
Myfirstfunction('小甲鱼')
>>> myfirstfunction('xiaojiayu')
传递进来的xiaojiayu叫做实参,因为ta时具体的参数值!

传递进来的‘小甲鱼’叫做实参,因为ta是具体的参数值!
函数文档
让别人更好的理解你的函数
>>> myfirstfunction.__doc__
'函数定义过程中的name是叫形参'
__doc__是函数的一个特殊属性,一般系统的特殊属性都是以双下横线开始和结束
利用help也可以看到文档
>>> help(myfirstfunction)
Help on function myfirstfunction in module __main__:

myfirstfunction(name)
    函数定义过程中的name是叫形参
>>> print.__doc__
"print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\nPrints the values to a stream, or to sys.stdout by default.\nOptional keyword arguments:\nfile:  a file-like object (stream); defaults to the current sys.stdout.\nsep:   string inserted between values, default a space.\nend:   string appended after the last value, default a newline.\nflush: whether to forcibly flush the stream."
>>> help(print)利用help可以把换行自动转义出来
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
   
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
关键字参数
>>> def saysome(name,words):
        print(name+'->'+words)

>>> saysome('xiaojiayu','let programming change the world')
xiaojiayu->let programming change the world
>>> saysome(words='let',name='xiaojiayu')
xiaojiayu->let
>>>
利用关键字可以使参数的输入不按照原顺序,参数的索引按照关键字来进行
关键字参数是在调用函数通过参数名制定需要赋值的参数
默认参数
定义了默认值的参数,当没有参数输入时,将带入默认参数
默认参数是在定义函数的过程中为形参赋初值,当函数调用忘了赋值的时候就会自动去找它的默认参数
>>> def saysome(name='xiaojiayu',words='let'):
        print(name+'->'+words)

>>> saysome()
xiaojiayu->let
>>> saysome('cangjingkong')
cangjingkong->let
>>> saysome('cangjingkong','我脱光衣服躺在镜头前,是为了生存,而你衣冠楚楚地站在镜头前,却只是为了私欲和欺骗')
cangjingkong->我脱光衣服躺在镜头前,是为了生存,而你衣冠楚楚地站在镜头前,却只是为了私欲和欺骗
收集参数(可变参数)
def test(*params):
        print('the length of the para is:',len(params))
        print('the second para is:',params[1])
>>> test(1,'lili',3.14,2,4,5,7)
the length of the para is: 7
the second para is: lili
如果函数有收集参数和其他参数,要为这些其他参数设置为默认参数,
>>> def test(*params,exp=8):
        print('the length of the para is:',len(params),exp)
        print('the second para is:',params[1])

       
>>> test(1,'lili',3.14,2,4,5,7)
the length of the para is: 7 8
the second para is: lili

评分

参与人数 2鱼币 +3 收起 理由
康小泡 + 1
小甲鱼 + 2 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 13:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表