mirror of https://github.com/TwoWater/Python
fix the format and typo
parent
39b1d5bad7
commit
23610146fa
|
@ -26,10 +26,10 @@
|
|||
```python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
def print_user_info( name , age , sex = '男' ):
|
||||
def print_user_info( name , age , sex='男' ):
|
||||
# 打印用户信息
|
||||
print('昵称:{}'.format(name) , end = ' ')
|
||||
print('年龄:{}'.format(age) , end = ' ')
|
||||
print('昵称:{}'.format(name) , end=' ')
|
||||
print('年龄:{}'.format(age) , end=' ')
|
||||
print('性别:{}'.format(sex))
|
||||
return;
|
||||
|
||||
|
@ -54,7 +54,7 @@ print_user_info( '三点水' , 25 )
|
|||
|
||||
默认值参数就这样结束了吗?
|
||||
|
||||
还没有的,细想一下,如果参数中是一个可修改的容器比如一个 lsit (列表)或者 dict (字典),那么我们使用什么来作为默认值呢?
|
||||
还没有的,细想一下,如果参数中是一个可修改的容器比如一个 list (列表)或者 dict (字典),那么我们使用什么来作为默认值呢?
|
||||
|
||||
我们可以使用 None 作为默认值。就像下面这个例子一样:
|
||||
|
||||
|
@ -87,7 +87,7 @@ def print_info( a , b = [] ):
|
|||
```python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
def print_info( a , b = [] ):
|
||||
def print_info( a , b=[] ):
|
||||
print(b)
|
||||
return b ;
|
||||
|
||||
|
@ -115,7 +115,7 @@ print_info(2)
|
|||
```python
|
||||
_no_value =object()
|
||||
|
||||
def print_info( a , b = _no_value ):
|
||||
def print_info( a , b=_no_value ):
|
||||
if b is _no_value :
|
||||
print('b 没有赋值')
|
||||
return;
|
||||
|
@ -143,17 +143,17 @@ def print_info( a , b = _no_value ):
|
|||
```python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
def print_user_info( name , age , sex = '男' ):
|
||||
def print_user_info( name , age , sex='男' ):
|
||||
# 打印用户信息
|
||||
print('昵称:{}'.format(name) , end = ' ')
|
||||
print('年龄:{}'.format(age) , end = ' ')
|
||||
print('昵称:{}'.format(name) , end=' ')
|
||||
print('年龄:{}'.format(age) , end=' ')
|
||||
print('性别:{}'.format(sex))
|
||||
return;
|
||||
|
||||
# 调用 print_user_info 函数
|
||||
|
||||
print_user_info( name = '两点水' ,age = 18 , sex = '女')
|
||||
print_user_info( name = '两点水' ,sex = '女', age = 18 )
|
||||
print_user_info( name = '两点水' ,age=18 , sex='女')
|
||||
print_user_info( name = '两点水' ,sex='女', age=18 )
|
||||
|
||||
```
|
||||
|
||||
|
@ -183,11 +183,11 @@ Python 提供了一种元组的方式来接受没有直接定义的参数。这
|
|||
```python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
def print_user_info( name , age , sex = '男' , * hobby):
|
||||
def print_user_info( name , age , sex='男' , * hobby):
|
||||
# 打印用户信息
|
||||
print('昵称:{}'.format(name) , end = ' ')
|
||||
print('年龄:{}'.format(age) , end = ' ')
|
||||
print('性别:{}'.format(sex) ,end = ' ' )
|
||||
print('昵称:{}'.format(name) , end=' ')
|
||||
print('年龄:{}'.format(age) , end=' ')
|
||||
print('性别:{}'.format(sex) ,end=' ' )
|
||||
print('爱好:{}'.format(hobby))
|
||||
return;
|
||||
|
||||
|
@ -213,11 +213,11 @@ print_user_info( '两点水' ,18 , '女', '打篮球','打羽毛球','跑步')
|
|||
```python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
def print_user_info( name , age , sex = '男' , ** hobby ):
|
||||
def print_user_info( name , age , sex='男' , ** hobby ):
|
||||
# 打印用户信息
|
||||
print('昵称:{}'.format(name) , end = ' ')
|
||||
print('年龄:{}'.format(age) , end = ' ')
|
||||
print('性别:{}'.format(sex) ,end = ' ' )
|
||||
print('昵称:{}'.format(name) , end=' ')
|
||||
print('年龄:{}'.format(age) , end=' ')
|
||||
print('性别:{}'.format(sex) ,end=' ' )
|
||||
print('爱好:{}'.format(hobby))
|
||||
return;
|
||||
|
||||
|
@ -245,15 +245,15 @@ print_user_info( name = '两点水' , age = 18 , sex = '女', hobby = ('打篮
|
|||
```python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
def print_user_info( name , *, age , sex = '男' ):
|
||||
def print_user_info( name , *, age , sex='男' ):
|
||||
# 打印用户信息
|
||||
print('昵称:{}'.format(name) , end = ' ')
|
||||
print('年龄:{}'.format(age) , end = ' ')
|
||||
print('昵称:{}'.format(name) , end=' ')
|
||||
print('年龄:{}'.format(age) , end=' ')
|
||||
print('性别:{}'.format(sex))
|
||||
return;
|
||||
|
||||
# 调用 print_user_info 函数
|
||||
print_user_info( name = '两点水' ,age = 18 , sex = '女' )
|
||||
print_user_info( name = '两点水' ,age=18 , sex='女' )
|
||||
|
||||
# 这种写法会报错,因为 age ,sex 这两个参数强制使用关键字参数
|
||||
#print_user_info( '两点水' , 18 , '女' )
|
||||
|
|
Loading…
Reference in New Issue