# 函数说明，高速闪光

### 布局参数

• 特色：金贝 MSN V-800 专门的学业影室闪光灯 八分一000s 高速闪光 持续时间

Python: 函数参数法力

2016-12-01

`````` >>> def myfun(x,y):
>>>     return x   y
``````

``````   >>> s = myfun(10)
---------------------------------------------------------------------------
TypeError  Traceback (most recent call last)
<ipython-input-18-135f1c06450c> in <module>()
----> 1 s = myfun(10)
TypeError: sum() missing 1 required positional argument: 'y'
``````

``````    >>> def myfun(x,y,z,h):
>>>     return (x   y)*z
``````

>>> def myfun(x,y,z = 1,h=0):
>>> return (x y)*z h

1）好处

2）注意点

• 必选参数在前，默许参数在后
• 充实暗中同意参数要思索不要影响旧代码的调用结果，大家得以因此增添私下认可参数扩充大家的函数作用
• 设若有多少个暗中同意参数，既能够遵从顺序 如myfun(10,20,三,8),也能够不遵照顺序，当不依据顺序传入时索要把参数名称写上如 myfun(拾,20,h=八),此时 z未有传来值，默许为1
• 记住一点：暗许参数必须指向不改变对象

1）定义

1

``````    >>> def myfun (number):
>>>     sum = 0
>>>     for i in number:
>>>         sum = sum   i*i
>>>      return sum
``````

2

number 是贰个list 大概tuple，那么大家调用的时候就须求创建成三个list或许tuple
>>> myfun([1,2,3])
>>> 14
2）用法

>>> def myfun (number):
>>> sum = 0
>>> for i in number:
>>> sum = sum i
i
>>> return sum

3

>>> myfun(1,3)
>>> 10
>>> myfun()
>>> 0

>>> num = [1,3]
>>> myfun(*num)
*nums代表把nums这几个list的全体因素作为可变参数字传送进去。那种写法万分有用，而且很广阔

4

** 四.重中之重字参数 **

5

``````  def myfun (name,email,**kw):
print('name:', name, 'email:', email, 'other:', kw)
``````

6

1. 用法
在概念关键字参数的时候 ，在函数的末段扩张**kw着重字就能够，函数内部，会把首要字参数转化为dict,大家在传唱的时候也要动用k=v传入

myfun('shuang','49206@qq.com',age=18,sex="女")
name: shuang email: 49206@qq.com other: {'sex': '女', 'age': 18}
笔者们也得以像可变参数3个 事先在函数外部定义好1个dict
extra = {'age': '18', 'sex': '女'}
myfun("shuang","49206@qq.com",**extra)
name: shuang email: 49206@qq.com other: {'age': '18', 'sex': '女'}
**extra表示把extra那么些dict的兼具key-value用关键字参数字传送入到函数的**kw参数，kw将获得3个dict。
注意：kw获得的dict是extra的1份拷贝，对kw的更改不会影响到函数外的extra

7

5.命名重中之重字参数

8

9

``````  >>> def myfun(name, email, **kw):
>>>     if 'age' in kw:
>>>       # 有age参数
>>>         pass
>>>     if 'sex' in kw:
>>>      # 有sex参数
>>>         pass
``````

10

11

``````def myfun(name, email, *, age, sex):
print(name, email, age, sex)
``````

12

3）用法

13

>>> def add(x, y):        # x, y 是必选参数

``````   >>> myfun('Jack', 'xx@qq.com', age=18, sex='女')
Jack xx@qq.com 18 女
``````

...     print x y

4）注意点

...

• 借使函数定义中早已有了2个可变参数，后边随着的命名第贰字参数就不再需求二个优秀分隔符*了：
>>> def myfun(name, email, *args, age, sex):
print(name, email, args, age, sex)

• 取名第二字参数必须传入参数名，那和岗位参数区别。假若未有传到参数名，调用将报错：

`````` >>> myfun('Jack',  'xx@qq.com', '18', '女')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: myfun() takes 2 positional arguments but 4 were given
``````

>>> add()                 # 啥都没传，不行

Traceback (most recent call last):

• 缺省值：
>>> def myfun(name, email, *, age='18', sex):
>>> print(name, email, age, sex)
由于命名第壹字参数age具备默许值，调用时，可不扩散age参数：

``````   >>> myfun('Jack', 'xxx@qq.com', sex='女')
Jack xxx@qq.com 18 女
``````
• 假定未有可变参数，就亟须加1个作为尤其分隔符。假若缺点和失误，Python解释器将无法辨识地点参数和命名首要字参数：

`````` def myfun(name, email, age, sex):
``````

缺少 *，age和sex被视为地点参数

File "", line 1, in

6.参数组合

TypeError: add() takes exactly 2 arguments (0 given)

>>> add(1)                # 只传了一个，也十三分

Traceback (most recent call last):

``````   def f1(a, b, c=0, *args, **kw):
print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)

def f2(a, b, c=0, *, d, **kw):
print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)
``````

File "", line 1, in

TypeError: add() takes exactly 2 arguments (1 given)

``````>>> f1(1, 2)
a = 1 b = 2 c = 0 args = () kw = {}
>>> f1(1, 2, c=3)
a = 1 b = 2 c = 3 args = () kw = {}
>>> f1(1, 2, 3, 'a', 'b')
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {}
>>> f1(1, 2, 3, 'a', 'b', x=99)
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {'x': 99}
>>> f2(1, 2, d=99, ext=None)
a = 1 b = 2 c = 0 d = 99 kw = {'ext': None}
``````

>>> add(1, 2)             # 数量同样，通过

3

`````` >>> args = (1, 2, 3, 4)
>>> kw = {'d': 99, 'x': '#'}
>>> f1(*args, **kw)
a = 1 b = 2 c = 3 args = (4,) kw = {'d': 99, 'x': '#'}
>>> args = (1, 2, 3)
>>> kw = {'d': 88, 'x': '#'}
>>> f2(*args, **kw)
a = 1 b = 2 c = 3 d = 88 kw = {'x': '#'}
``````

1

2

3

4

5

6

7

>>> def add(x, y, z=1):     # x, y 是必选参数，z 是暗中认可参数，私下认可值是 一

...     print x y z

...

>>> add(1, 2, 3)            # 1 2 3

6

>>> add(1, 2)               # 未有传递 z，自动使用 z=1，即 一 2 壹

4

1

2

3

4

5

6

7

8

9

10

11

>>> def add(x=1, y, z):      # x 是暗中认可参数，必须放在全部必选参数的前边

...     return x y z

...

File "", line 1

SyntaxError: non-default argument follows default argument

>>>

>>> def add(x, y=1, z):      # y 是暗中认可参数，必须放在全部必选参数的末端

...     return x y z

...

File "", line 1

SyntaxError: non-default argument follows default argument

1

2

3

...     L.append('END')

...     return L

1

2

3

4

5

6

7

8

9

10

>>> add_to_list([1, 2, 3])          # 没啥难点

[1, 2, 3, 'END']

>>> add_to_list(['a', 'b', 'c'])    # 没啥难点

['a', 'b', 'c', 'END']

>>> add_to_list()                   # 未有传递参数，使用默许值，也没啥难题

['END']

>>> add_to_list()                   # 未有传递参数，使用暗许值，竟出现八个 'END'

['END', 'END']

>>> add_to_list()                   # 糟糕了，三个 'END'

['END', 'END', 'END']

1

2

3

4

5

6
{"ft_lang_label":"__label__zh","ft_lang_prob":0.5437047,"math_prob":0.9421104,"size":6370,"snap":"2019-35-2019-39","text_gpt3_token_len":4139,"char_repetition_ratio":0.11875589,"word_repetition_ratio":0.19763313,"special_character_ratio":0.3846154,"punctuation_ratio":0.21647818,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97140694,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-21T05:33:15Z\",\"WARC-Record-ID\":\"<urn:uuid:ef3e3cf2-068c-4944-bc0a-585d0722b44c>\",\"Content-Length\":\"21739\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:79572414-92c9-42a2-b1be-0411ed7afd34>\",\"WARC-Concurrent-To\":\"<urn:uuid:05faff06-9072-42fa-9d58-67da127ec591>\",\"WARC-IP-Address\":\"156.237.150.122\",\"WARC-Target-URI\":\"https://www.htianz.com/shenghuozixun/173.html\",\"WARC-Payload-Digest\":\"sha1:RPX5FOPJ4BITFRY43CMDLSXPAAGIUYTF\",\"WARC-Block-Digest\":\"sha1:T5JWJSYOEBKSK6B2BXPICNRRBXN5VJX4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514574265.76_warc_CC-MAIN-20190921043014-20190921065014-00295.warc.gz\"}"}