Created
December 26, 2018 05:31
-
-
Save caoya171193579/8bc2aba4ab0ad4807f28f5f06863e99e to your computer and use it in GitHub Desktop.
进阶
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
案例:(多类型传值、传值冗余) | |
多类型传值: | |
1、向函数传元祖和字典 (传值的时候,*一个代表元祖,**两个代表字典) | |
2、处理多余实参 | |
############################################# | |
>>> print "%s : %s" % (12,23) # 一个%s 代表一个元素, 用%来传 | |
12 : 23 | |
print打印的值是最后赋给它的用("%s : %s" %)的形式传值,值也要用括号() 来定义。 | |
>>>t = 'name',"milo" #t 等于两个字符串 | |
>>> t | |
('name', 'milo') | |
>>> def d(x,y): #定义一个函数 | |
... pront "%s : %s" % x,y #这里用到了传值,调用函数d的时候实际参数会被打印出来,这里有个语法错误的问题,最后的传值 : x,y 是要加上() 做为元祖的,或者字典都可以 {} . | |
File "<stdin>", line 2 | |
pront "%s : %s" % x,y | |
^ | |
SyntaxError: invalid syntax | |
>>> def d(x,y): | |
... print "%s : %s" % (x,y) | |
... | |
>>> d(*t) #t 之前已经定义为两个字符串,所以代表两个值,单独做实参会报错, 这里用到了 * 星号来表示 t它是一个元祖 | |
name : milo | |
>>> a = 520,1314 | |
>>> a | |
(520, 1314) | |
>>> d(*a) | |
520 : 1314 | |
>>> | |
######################################################## | |
>>> def d(name="name",nianling=0): #定义一个函数d ,形式参数为name和nianliang,默认的值对应的是"name" 和 0. | |
... print "name : %s" % name #打印不确定name的值,最后调用的时候实际参数会被打印出来 | |
... print "nianling : %s" % nianling | |
... | |
>>> d() #没给实参,打印出来的是默认值。 | |
name : name | |
nianling : 0 | |
>>> d(caoya,28) #英文未加""或'' 而报错 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
NameError: name 'caoya' is not defined | |
>>> d('caoya',28) | |
name : caoya | |
nianling : 28 | |
>>> | |
#元祖是有序的!全都是位置对应的 | |
########################################################### | |
>>> a = {'nianling':28,'name':'caoya'} #定义一个字典 | |
>>> a | |
{'nianling': 28, 'name': 'caoya'} | |
>>> d(**a) #调用字典的时候要用** 两个星号来识别。 | |
name : caoya | |
nianling : 28 | |
这个字典里面的实参key 和 函数 里面的形参 key 是对应的 ,所以可以直接调用。 | |
>>>a['nianling']=31 >>>a['name']='dandan' | |
>>>a >>>a | |
name : caoya name : dandan | |
nianling : 31 nianling : 31 #字典的值可以任意更改,对函数无任何影响。 | |
多类型传值 | |
从新定义一个字典: | |
>>> z = {'f':60,'g':'qiqi'} | |
>>> z | |
{'g': 'qiqi', 'f': 60} # 字典是无序,没有顺序的,是对应调用的。 | |
>>> d(**z) #字典z 里面的key ,和 函数的key不对应,调用的时候报错 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: d() got an unexpected keyword argument 'g' | |
>>> d(z['g'],z['f']) # 这种类型的传值,要这样用,实参的第一个值取z的'g',第二个值取'f' | |
name : qiqi | |
nianling : 60 | |
>>> | |
###################################################### | |
冗余、处理多余实参: (fun (*args)#星号加元祖 、fun (**kwords)#双星号加字典。。。元祖和字典名可以是自由定义的任何) | |
>>> def a(x,*kk): #定义函数a 形参为x,和一个元祖kk | |
... print x | |
... print kk | |
... | |
>>> a() #没有传任何值所以报错。 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: a() takes at least 1 argument (0 given) | |
>>> a(1) | |
1 #传值1 赋值给x,元祖默认就是() | |
() | |
>>> a(1,43,55,667,776876,'sddafwf') #给字典传多个值,只有第一位对应x, 其余的冗余 都传给了元祖里面。 | |
1 | |
(43, 55, 667, 776876, 'sddafwf') | |
>>> | |
############################################### | |
>>> def f(x,*kk,**yy): #定义函数f, 形参为x, 元祖kk, 字典yy | |
... print x | |
... print kk | |
... print yy #分别打印 | |
... | |
>>> f(1) #传一个值的时候只会赋给x,kk默认(),yy默认{} | |
1 | |
() #元祖 | |
{} #字典 | |
>>> f(1,2,3,4,5,x=6,y=10,z=20) #这个报错是因为第一个值已经赋给了x,后面x=6是不能成立的,不能同时赋给x两个值。 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: f() got multiple values for keyword argument 'x' | |
>>> f(1,2,3,4,5,'x',y=10,z=20) | |
1 | |
(2, 3, 4, 5, 'x') | |
{'y': 10, 'z': 20} #1赋给了x,多余的参数2,3,4,5,'x'赋给了元祖,带等值的y=10,z=20赋给了字典 | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment