Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the format and typo #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 23 additions & 23 deletions Article/PythonBasis/python6/3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -54,7 +54,7 @@ print_user_info( '三点水' , 25 )

默认值参数就这样结束了吗?

还没有的,细想一下,如果参数中是一个可修改的容器比如一个 lsit (列表)或者 dict (字典),那么我们使用什么来作为默认值呢?
还没有的,细想一下,如果参数中是一个可修改的容器比如一个 list (列表)或者 dict (字典),那么我们使用什么来作为默认值呢?

我们可以使用 None 作为默认值。就像下面这个例子一样:

Expand Down Expand Up @@ -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 ;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 )

```

Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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 , '女' )
Expand Down