Python 函数

PEP 8 - Style Guide for Python Code
https://www.python.org/dev/peps/pep-0008/

函数是带名字的代码块,用于完成具体的工作。

模块是扩展名为 .py 的文件,包含要导入到程序中的代码。将函数存储在被称为模块的独立文件中,让主程序文件的组织更为有序。

1. 定义函数

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def greet_user():
    """Display a simple greeting."""
    print("Hello, yongqiang!")


if __name__ == '__main__':
    greet_user()
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
Hello, yongqiang!

Process finished with exit code 0

使用关键字 def 来告诉 Python 你要定义一个函数。这是函数定义,向 Python 指出了函数名,还可能在括号内指出函数为完成其任务需要什么样的信息。在这里,函数名为 greet_user(),它不需要任何信息就能完成其工作,因此括号是空的 (即便如此,括号也必不可少)。最后,定义以冒号结尾。

紧跟在 def greet_user(): 后面的所有缩进行构成了函数体。"""Display a simple greeting.""" 的文本是被称为文档字符串 (docstring) 的注释,描述了函数是做什么的。文档字符串用三引号括起,Python 使用它们来生成有关程序中函数的文档。

要调用函数,可依次指定函数名以及用括号括起的必要信息。由于这个函数不需要任何信息,因此调用它时只需输入 greet_user() 即可。

1.1. 向函数传递信息

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def greet_user(username):
    """Display a simple greeting."""
    print("Hello, " + username.title() + "!")


if __name__ == '__main__':
    greet_user("yongqiang")

/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
Hello, Yongqiang!

Process finished with exit code 0

你可以根据需要调用函数 greet_user(username) 任意次,调用时无论传入什么样的名字,都会生成相应的输出。

1.2. 实参和形参

定义函数 greet_user(username) 时,要求给变量 username 指定一个值。在函数 greet_user(username) 的定义中,变量 username 是一个形参,函数完成其工作所需的一项信息。在代码 greet_user("yongqiang") 中,值 "yongqiang" 是一个实参。实参是调用函数时传递给函数的信息。我们调用函数时,将要让函数使用的信息放在括号内。在 greet_user("yongqiang") 中,将实参 "yongqiang" 传递给了函数 greet_user(username),这个值被存储在形参 username 中。

2. 传递实参

函数定义中可能包含多个形参,因此函数调用中也可能包含多个实参。向函数传递实参的方式很多,可使用位置实参,这要求实参的顺序与形参的顺序相同。也可使用关键字实参,其中每个实参都由变量名和值组成,还可使用列表和字典。

2.1. 位置实参

调用函数时,Python 必须将函数调用中的每个实参都关联到函数定义中的一个形参。最简单的关联方式是基于实参的顺序,这种关联方式被称为位置实参。

hamster [ˈhæmstə(r)]:n. 仓鼠,仓鼠毛皮
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def describe_pet(animal_type, pet_name):
    """Display information about a pet."""
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")


if __name__ == '__main__':
    describe_pet("hamster", "harry")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow

I have a hamster.
My hamster's name is Harry.

Process finished with exit code 0

这个函数的定义表明,它需要一种动物类型和一个名字。调用 describe_pet(animal_type, pet_name) 时,需要按顺序提供一种动物类型和一个名字。例如,在前面的函数调用中,实参 "hamster" 存储在形参 animal_type 中,而实参 "harry" 存储在形参 pet_name 中。在函数体内,使用了这两个形参来显示宠物的信息。

1. 调用函数多次

你可以根据需要调用函数任意次。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def describe_pet(animal_type, pet_name):
    """Display information about a pet."""
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")


if __name__ == '__main__':
    describe_pet("hamster", "harry")
    describe_pet("dog", "willie")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow

I have a hamster.
My hamster's name is Harry.

I have a dog.
My dog's name is Willie.

Process finished with exit code 0

第二次调用 describe_pet(animal_type, pet_name) 函数时,我们向它传递了实参 "dog""willie"。与第一次调用时一样,Python 将实参 "dog" 关联到形参 animal_type,并将实参 "willie" 关联到形参 pet_name

调用函数多次是一种效率极高的工作方式。我们只需在函数中编写描述宠物的代码一次,然后每当需要描述新宠物时,都可调用这个函数,并向它提供新宠物的信息。

在函数中,可根据需要使用任意数量的位置实参,Python 将按顺序将函数调用中的实参关联到函数定义中相应的形参。

2. 位置实参的顺序很重要

使用位置实参来调用函数时,如果实参的顺序不正确,结果可能出乎意料。请确认函数调用中实参的顺序与函数定义中形参的顺序一致。

2.2. 关键字实参

关键字实参是传递给函数的名称 - 值对。你直接在实参中将名称和值关联起来了,因此向函数传递实参时不会混淆。关键字实参让你无需考虑函数调用中的实参顺序,还清楚地指出了函数调用中各个值的用途。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def describe_pet(animal_type, pet_name):
    """Display information about a pet."""
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")


if __name__ == '__main__':
    describe_pet(animal_type="hamster", pet_name="harry")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow

I have a hamster.
My hamster's name is Harry.

Process finished with exit code 0

函数 describe_pet(animal_type, pet_name) 还是原来那样,但调用这个函数时,我们向 Python 明确地指出了各个实参对应的形参。看到这个函数调用时,Python 知道应该将实参 "hamster""harry" 分别存储在形参 animal_typepet_name 中。

关键字实参的顺序无关紧要,因为 Python 知道各个值该存储到哪个形参中。下面两个函数调用是等效的:

describe_pet(animal_type="hamster", pet_name="harry")
describe_pet(pet_name="harry", animal_type="hamster")

注意使用关键字实参时,务必准确地指定函数定义中的形参名。

2.3. 默认值

编写函数时,可给每个形参指定默认值。在调用函数中给形参提供了实参时,Python 将使用指定的实参值;否则,将使用形参的默认值。因此,给形参指定默认值后,可在函数调用中省略相应的实参。使用默认值可简化函数调用,还可清楚地指出函数的典型用法。

形参 animal_type 的默认值设置为 "dog"。这样,调用 describe_pet(pet_name, animal_type="dog") 来描述小狗时,就可不提供这种信息:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def describe_pet(pet_name, animal_type="dog"):
    """Display information about a pet."""
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")


if __name__ == '__main__':
    # A dog named Willie.
    describe_pet("willie")
    describe_pet(pet_name="willie")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow

I have a dog.
My dog's name is Willie.

I have a dog.
My dog's name is Willie.

Process finished with exit code 0

这里修改了函数 describe_pet(pet_name, animal_type="dog") 的定义,在其中给形参 animal_type 指定了默认值 "dog"。这样,调用这个函数时,如果没有给 animal_type 指定值,Python 将把这个形参设置为 "dog"

请注意,在这个函数的定义中,修改了形参的排列顺序。由于给 animal_type 指定了默认值,无需通过实参来指定动物类型,因此在函数调用中只包含一个实参 - 宠物的名字。然而,Python 依然将这个实参视为位置实参,因此如果函数调用中只包含宠物的名字,这个实参将关联到函数定义中的第一个形参。这就是需要将 pet_name 放在形参列表开头的原因所在。

现在,使用这个函数的最简单的方式是,在函数调用中只提供小狗的名字 describe_pet("willie")
这个函数调用的输出与前一个示例相同。只提供了一个实参 - "willie",这个实参将关联到函数定义中的第一个形参 - pet_name。由于没有给 animal_type 提供实参,因此 Python 使用其默认值 "dog"

如果要描述的动物不是小狗,可使用类似于下面的函数调用:

describe_pet(pet_name='harry', animal_type='hamster')

由于显式地给 animal_type 提供了实参,因此 Python 将忽略这个形参的默认值。

注意使用默认值时,在形参列表中必须先列出没有默认值的形参,再列出有默认值的形参。这让 Python 依然能够正确地解读位置实参。

2.4. 等效的函数调用

鉴于可混合使用位置实参、关键字实参和默认值,通常有多种等效的函数调用方式。请看下面的函数定义,其中给一个形参提供了默认值:

def describe_pet(pet_name, animal_type='dog'):

基于这种定义,在任何情况下都必须给 pet_name 提供实参。指定该实参时可以使用位置方式,也可以使用关键字方式。如果要描述的动物不是小狗,还必须在函数调用中给 animal_type 提供实参。指定该实参时可以使用位置方式,也可以使用关键字方式。

下面对这个函数的所有调用都可行:

# 一条名为 Willie 的小狗
describe_pet('willie')
describe_pet(pet_name='willie')
# 一只名为 Harry 的仓鼠
describe_pet('harry', 'hamster')
describe_pet(pet_name='harry', animal_type='hamster')
describe_pet(animal_type='hamster', pet_name='harry')

使用对你来说最容易理解的调用方式即可。

2.5. 避免实参错误

你提供的实参多于或少于函数完成其工作所需的信息时,将出现实参不匹配错误。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def describe_pet(animal_type, pet_name):
    """Display information about a pet."""
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")


if __name__ == '__main__':
    describe_pet()
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
Traceback (most recent call last):
  File "/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py", line 26, in <module>
    describe_pet()
TypeError: describe_pet() takes exactly 2 arguments (0 given)

Process finished with exit code 1

traceback 指出了问题出在什么地方,让我们能够回过头去找出函数调用中的错误。traceback 指出该函数调用少两个实参。

如果提供的实参太多,将出现类似的 traceback,帮助你确保函数调用和函数定义匹配。

3. 返回值

函数并非总是直接显示输出,相反,它可以处理一些数据,并返回一个或一组值。函数返回的值被称为返回值。在函数中,可使用 return 语句将值返回到调用函数的代码行。返回值让你能够将程序的大部分繁重工作移到函数中去完成,从而简化主程序。

3.1. 返回简单值

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def get_formatted_name(first_name, last_name):
    """Return a full name, neatly formatted."""
    full_name = first_name + ' ' + last_name
    return full_name.title()


if __name__ == '__main__':
    musician = get_formatted_name('jimi', 'hendrix')
    print(musician)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
Jimi Hendrix

Process finished with exit code 0

函数 get_formatted_name(first_name, last_name) 的定义通过形参接受名和姓。它将姓和名合而为一,在它们之间加上一个空格,并将结果存储在变量 full_name 中。然后,将 full_name 的值转换为首字母大写格式,并将结果返回到函数调用行。

调用返回值的函数时,需要提供一个变量,用于存储返回的值。在这里,将返回值存储在了变量 musician 中。输出为整洁的姓名 Jimi Hendrix

你分别存储名和姓,每当需要显示姓名时都调用这个函数。

3.2. 让实参变成可选的

有时候,需要让实参变成可选的,这样使用函数的人就只需在必要时才提供额外的信息。可使用默认值来让实参变成可选的。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def get_formatted_name(first_name, middle_name, last_name):
    """Return a full name, neatly formatted."""
    full_name = first_name + ' ' + middle_name + ' ' + last_name
    return full_name.title()


if __name__ == '__main__':
    musician = get_formatted_name('john', 'lee', 'hooker')
    print(musician)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
John Lee Hooker

Process finished with exit code 0

只要同时提供名、中间名和姓,这个函数就能正确地运行。它根据这三部分创建一个字符串,在适当的地方加上空格,并将结果转换为首字母大写格式 John Lee Hooker

然而,并非所有的人都有中间名,但如果你调用这个函数时只提供了名和姓,它将不能正确地运行。为让中间名变成可选的,可给实参 middle_name 指定一个默认值 - 空字符串,并在用户没有提供中间名时不使用这个实参。为让函数在没有提供中间名时依然可行,可给实参 middle_name 指定一个默认值 - 空字符串,并将其移到形参列表的末尾:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def get_formatted_name(first_name, last_name, middle_name=''):
    """Return a full name, neatly formatted."""
    if middle_name:
        full_name = first_name + ' ' + middle_name + ' ' + last_name
    else:
        full_name = first_name + ' ' + last_name
    return full_name.title()


if __name__ == '__main__':
    musician = get_formatted_name('jimi', 'hendrix')
    print(musician)

    musician = get_formatted_name('john', 'hooker', 'lee')
    print(musician)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
Jimi Hendrix
John Lee Hooker

Process finished with exit code 0

在这个示例中,姓名是根据三个可能提供的部分创建的。由于人都有名和姓,因此在函数定义中首先列出了这两个形参。中间名是可选的,因此在函数定义中最后列出该形参,并将其默认值设置为空字符串。
在函数体中,我们检查是否提供了中间名。Python 将非空字符串解读为 True,因此如果函数调用中提供了中间名,if middle_name 将为 True。如果提供了中间名,就将名、中间名和姓合并为姓名,然后将其修改为首字母大写格式,并返回到函数调用行。在函数调用行,将返回的值存储在变量 musician 中;然后将这个变量的值打印出来。如果没有提供中间名,middle_name 将为空字符串,导致 if 测试未通过,进而执行 else 代码块:只使用名和姓来生成姓名,并将设置好格式的姓名返回给函数调用行。在函数调用行, 将返回的值存储在变量 musician 中; 然后将这个变量的值打印出来。

调用这个函数时,如果只想指定名和姓,调用起来将非常简单。如果还要指定中间名,就必须确保它是最后一个实参,这样 Python 才能正确地将位置实参关联到形参,这个修改后的版本适用于只有名和姓的人,也适用于还有中间名的人。

可选值让函数能够处理各种不同情形的同时,确保函数调用尽可能简单。

3.3. 返回字典

函数可返回任何类型的值,包括列表和字典等较复杂的数据结构。例如,下面的函数接受姓名的组成部分,并返回一个表示人的字典:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def build_person(first_name, last_name):
    """Return a dictionary of information about a person."""
    person = {'first': first_name, 'last': last_name}
    return person


if __name__ == '__main__':
    musician = build_person('jimi', 'hendrix')
    print(musician)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
{'last': 'hendrix', 'first': 'jimi'}

Process finished with exit code 0

函数接受名和姓,并将这些值封装到字典中。存储 first_name 的值时,使用的键为 'first',而存储 last_name 的值时,使用的键为 'last'。最后,返回表示人的整个字典。此时原来的两项文本信息存储在一个字典中 {'last': 'hendrix', 'first': 'jimi'}

这个函数接受简单的文本信息,将其放在一个更合适的数据结构中,让你不仅能打印这些信息,还能以其他方式处理它们。当前,字符串 'jimi''hendrix' 被标记为名和姓。你可以轻松地扩展这个函数,使其接受可选值,如中间名、年龄、职业或你要存储的其他任何信息。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def build_person(first_name, last_name, age=''):
    """Return a dictionary of information about a person."""
    person = {'first': first_name, 'last': last_name}
    if age:
        person['age'] = age
    return person


if __name__ == '__main__':
    musician = build_person('jimi', 'hendrix', age=27)
    print(musician)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
{'age': 27, 'last': 'hendrix', 'first': 'jimi'}

Process finished with exit code 0

在函数定义中,我们新增了一个可选形参 age,并将其默认值设置为空字符串。如果函数调用中包含这个形参的值,这个值将存储到字典中。在任何情况下,这个函数都会存储人的姓名,但可对其进行修改,使其也存储有关人的其他信息。

3.4. 结合使用函数和 while 循环

我们要让用户能够尽可能容易地退出,因此每次提示用户输入时,都应提供退出途径。每次提示用户输入时,都使用 break 语句提供了退出循环的简单途径:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def get_formatted_name(first_name, last_name):
    """Display a simple greeting."""
    full_name = first_name + ' ' + last_name
    return full_name.title()


if __name__ == '__main__':
    while True:
        print("\nPlease tell me your name:")
        print("(enter 'q' at any time to quit)")

        f_name = input("First name: ")
        if f_name == 'q':
            break

        l_name = input("Last name: ")
        if l_name == 'q':
            break

        formatted_name = get_formatted_name(f_name, l_name)
        print("\nHello, " + formatted_name + "!")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow

Please tell me your name:
(enter 'q' at any time to quit)
First name: "yongqiang"
Last name: "cheng"

Hello, Yongqiang Cheng!

Please tell me your name:
(enter 'q' at any time to quit)
First name: "q"

Process finished with exit code 0

我们添加了一条消息来告诉用户如何退出,然后在每次提示用户输入时,都检查他输入的是否是退出值,如果是,就退出循环。这个程序将不断地问候,直到用户输入的姓或名为 'q' 为止。

strong@foreverstrong:~/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow$ python yongqiang.py 
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow

Please tell me your name:
(enter 'q' at any time to quit)
First name: "yongqiang"
Last name: "cheng"

Hello, Yongqiang Cheng!

Please tell me your name:
(enter 'q' at any time to quit)
First name: "q"
strong@foreverstrong:~/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow$

4. 传递列表

将列表传递给函数后,函数就能直接访问其内容。我们将 greet_users(names) 定义成接受一个名字列表,并将其存储在形参 names 中。这个函数遍历收到的列表。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def greet_users(names):
    """Print a simple greeting to each user in the list."""
    for name in names:
        msg = "Hello, " + name.title() + "!"
        print(msg)


if __name__ == '__main__':
    usernames = ['hannah', 'ty', 'margot']
    greet_users(usernames)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
Hello, Hannah!
Hello, Ty!
Hello, Margot!

Process finished with exit code 0

4.1. 在函数中修改列表

将列表传递给函数后,函数就可对其进行修改。在函数中对这个列表所做的任何修改都是永久性的,这让你能够高效地处理大量的数据。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def print_models(unprinted_designs, completed_models):
    """
    Simulate printing each design, until there are none left.
    Move each design to completed_models after printing.
    """
    while unprinted_designs:
        current_design = unprinted_designs.pop()

        # Simulate creating a 3d print from the design.
        print("Printing model: " + current_design)
        completed_models.append(current_design)


def show_completed_models(completed_models):
    """Show all the models that were printed."""
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)


if __name__ == '__main__':
    unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
    completed_models = []

    print_models(unprinted_designs, completed_models)
    show_completed_models(completed_models)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
Printing model: dodecahedron
Printing model: robot pendant
Printing model: iphone case

The following models have been printed:
dodecahedron
robot pendant
iphone case

Process finished with exit code 0

我们定义了函数 print_models(unprinted_designs, completed_models),它包含两个形参:一个需要打印的设计列表和一个打印好的模型列表。给定这两个列表,这个函数模拟打印每个设计的过程:将设计逐个地从未打印的设计列表中取出,并加入到打印好的模型列表中。我们定义了函数 show_completed_models(completed_models),它包含一个形参:打印好的模型列表。给定这个列表,函数 show_completed_models(completed_models) 显示打印出来的每个模型的名称。这个程序的输出与未使用函数的版本相同,但组织更为有序。完成大部分工作的代码都移到了两个函数中,让主程序更容易理解。只要看看主程序,你就知道这个程序的功能容易看清得多:

unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)

我们创建了一个未打印的设计列表,还创建了一个空列表,用于存储打印好的模型。接下来,由于我们已经定义了两个函数,因此只需调用它们并传入正确的实参即可。我们调用 print_models(unprinted_designs, completed_models) 并向它传递两个列表;像预期的一样,print_models(unprinted_designs, completed_models) 模拟打印设计的过程。接下来,我们调用 show_completed_models(completed_models),并将打印好的模型列表传递给它,让其能够指出打印了哪些模型。描述性的函数名让别人阅读这些代码时也能明白,虽然其中没有任何注释。

相比于没有使用函数的版本,这个程序更容易扩展和维护。如果以后需要打印其他设计,只需再次调用 print_models(unprinted_designs, completed_models) 即可。如果我们发现需要对打印代码进行修改,只需修改这些代码一次,就能影响所有调用该函数的地方;与必须分别修改程序的多个地方相比,这种修改的效率更高。

这个程序还演示了这样一种理念,即每个函数都应只负责一项具体的工作。第一个函数打印每个设计,而第二个显示打印好的模型;这优于使用一个函数来完成两项工作。 编写函数时, 如果你发现它执行的任务太多,请尝试将这些代码划分到两个函数中。别忘了,总是可以在一个函数中调用另一个函数,这有助于将复杂的任务划分成一系列的步骤。

4.2. 禁止函数修改列表

禁止函数修改列表,可向函数传递列表的副本而不是原件;这样函数所做的任何修改都只影响副本,而丝毫不影响原件。
要将列表的副本传递给函数, 可以像下面这样做:

function_name(list_name[:])

切片表示法 [:] 创建列表的副本。如果不想清空未打印的设计列表,可像下面这样调用 print_models():

print_models(unprinted_designs[:], completed_models)

这样函数 print_models(unprinted_designs, completed_models) 依然能够完成其工作,因为它获得了所有未打印的设计的名称, 但它使用的是列表 unprinted_designs 的副本, 而不是列表 unprinted_designs 本身。像以前一样,列表 completed_models 也将包含打印好的模型的名称,但函数所做的修改不会影响到列表 unprinted_designs

虽然向函数传递列表的副本可保留原始列表的内容,但除非有充分的理由需要传递副本,否则还是应该将原始列表传递给函数,因为让函数使用现成列表可避免花时间和内存创建副本,从而提高效率,在处理大型列表时尤其如此。

5. 传递任意数量的实参

有时候,你预先不知道函数需要接受多少个实参,好在 Python 允许函数从调用语句中收集任意数量的实参。例如,来看一个制作比萨的函数,它需要接受很多配料,但你无法预先确定顾客要多少种配料。下面的函数只有一个形参 *toppings,但不管调用语句提供了多少实参,这个形参都将它们统统收入囊中:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def make_pizza(*toppings):
    """Summarize the pizza we are about to make."""
    print(toppings)


if __name__ == '__main__':
    make_pizza('pepperoni')
    make_pizza('mushrooms', 'green peppers', 'extra cheese')
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
('pepperoni',)
('mushrooms', 'green peppers', 'extra cheese')

Process finished with exit code 0

形参名 *toppings 中的星号让 Python 创建一个名为 toppings 的空元组,并将收到的所有值都封装到这个元组中。函数体内的 print 语句通过生成输出来证明 Python 能够处理使用一个值调用函数的情形,也能处理使用三个值来调用函数的情形。它以类似的方式处理不同的调用,注意 Python 将实参封装到一个元组中,即便函数只收到一个值也如此。

现在,我们可以将这条 print 语句替换为一个循环,对配料列表进行遍历,并对顾客点的比萨进行描述:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def make_pizza(*toppings):
    """Summarize the pizza we are about to make."""
    print("\nMaking a pizza with the following toppings:")
    for topping in toppings:
        print("- " + topping)


if __name__ == '__main__':
    make_pizza('pepperoni')
    make_pizza('mushrooms', 'green peppers', 'extra cheese')
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow

Making a pizza with the following toppings:
- pepperoni

Making a pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese

Process finished with exit code 0

不管收到的是一个值还是三个值,这个函数都能妥善地处理,不管函数收到的实参是多少个,这种语法都管用。

5.1. 结合使用位置实参和任意数量实参

如果要让函数接受不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最后。Python 先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中。例如,如果前面的函数还需要一个表示比萨尺寸的实参,必须将该形参放在形参 *toppings 的前面:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def make_pizza(size, *toppings):
    """Summarize the pizza we are about to make."""
    print("\nMaking a " + str(size) + "-inch pizza with the following toppings:")
    for topping in toppings:
        print("- " + topping)


if __name__ == '__main__':
    make_pizza(16, 'pepperoni')
    make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow

Making a 16-inch pizza with the following toppings:
- pepperoni

Making a 12-inch pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese

Process finished with exit code 0

基于上述函数定义,Python 将收到的第一个值存储在形参 size 中,并将其他的所有值都存储在元组 toppings 中。在函数调用中,首先指定表示比萨尺寸的实参,然后根据需要指定任意数量的配料。

5.2. 使用任意数量的关键字实参

有时候,需要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息。在这种情况下,可将函数编写成能够接受任意数量的键 - 值对,调用语句提供了多少就接受多少。在下面的示例中,函数 build_profile(first, last, **user_info) 接受名和姓,同时还接受任意数量的关键字实参:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)


def build_profile(first, last, **user_info):
    """Build a dictionary containing everything we know about a user."""
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last
    for key, value in user_info.items():
        profile[key] = value
    return profile


if __name__ == '__main__':
    user_profile = build_profile('albert', 'einstein', location='princeton', field='physics')
    print(user_profile)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
{'field': 'physics', 'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton'}

Process finished with exit code 0

函数 build_profile(first, last, **user_info) 的定义要求提供名和姓,同时允许用户根据需要提供任意数量的名称 - 值对。形参 **user_info 中的两个星号让 Python 创建一个名为 user_info 的空字典,并将收到的所有名称 - 值对都封装到这个字典中。在这个函数中,可以像访问其他字典那样访问 user_info 中的名称 - 值对。

build_profile(first, last, **user_info) 的函数体内,我们创建了一个名为 profile 的空字典,用于存储用户简介。我们遍历字典 user_info 中的键 - 值对,并将每个键 - 值对都加入到字典 profile 中。最后,我们将字典 profile 返回给函数调用行。

我们调用 build_profile(first, last, **user_info),向它传递名 ('albert')、姓 ('einstein') 和两个键 - 值对 (location='princeton'field='physics'),并将返回的 profile 存储在变量 user_profile 中,再打印这个变量。

调用这个函数时,不管额外提供了多少个键 - 值对,它都能正确地处理。

编写函数时,你可以以各种方式混合使用位置实参、关键字实参和任意数量的实参。知道这些实参类型大有裨益,因为阅读别人编写的代码时经常会见到它们。要正确地使用这些类型的实参并知道它们的使用时机,需要经过一定的练习。

6. 将函数存储在模块中

函数的优点之一是,使用它们可将代码块与主程序分离。通过给函数指定描述性名称,可让主程序容易理解得多。你还可以更进一步,将函数存储在被称为模块的独立文件中,再将模块导入到主程序中。import 语句允许在当前运行的程序文件中使用模块中的代码。

通过将函数存储在独立的文件中,可隐藏程序代码的细节,将重点放在程序的高层逻辑上。这还能让你在众多不同的程序中重用函数。将函数存储在独立文件中后,可与其他程序员共享这些文件而不是整个程序。

6.1. 导入整个模块

要让函数是可导入的,得先创建模块。模块是扩展名为 .py 的文件,包含要导入到程序中的代码。下面来创建一个包含函数 make_pizza(size, *toppings) 的模块。

pizza.py

def make_pizza(size, *toppings):
    """Summarize the pizza we are about to make."""
    print("\nMaking a " + str(size) + "-inch pizza with the following toppings:")
    for topping in toppings:
        print("- " + topping)

接下来,我们在 pizza.py 所在的目录中创建另一个名为 making_pizzas.py 的文件,这个文件导入刚创建的模块,再调用 make_pizza(size, *toppings) 两次:
making_pizzas.py

import pizza as p

p.make_pizza(16, 'pepperoni')
p.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

Python 读取这个文件时,代码行 import pizza 让 Python 打开文件 pizza.py,并将其中的所有函数都复制到这个程序中。你看不到复制的代码,因为这个程序运行时,Python 在幕后复制这些代码。你只需知道,在 making_pizzas.py 中,可以使用 pizza.py 中定义的所有函数。

要调用被导入的模块中的函数,可指定导入的模块的名称 pizza 和函数名 make_pizza(),并用句点分隔它们。这些代码的输出与没有导入模块的原始程序相同:

Making a 16-inch pizza with the following toppings:
- pepperoni

Making a 12-inch pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese

这就是一种导入方法:只需编写一条 import 语句并在其中指定模块名,就可在程序中使用该模块中的所有函数。如果你使用这种 import 语句导入了名为 module_name.py 的整个模块,就可使用下面的语法来使用其中任何一个函数:

module_name.function_name()

6.2. 导入特定的函数

你还可以导入模块中的特定函数,这种导入方法的语法如下:

from module_name import function_name

通过用逗号分隔函数名,可根据需要从模块中导入任意数量的函数:

from module_name import function_0, function_1, function_2

对于前面的 making_pizzas.py 示例,如果只想导入要使用的函数,代码将类似于下面这样:

from pizza import make_pizza
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

若使用这种语法,调用函数时就无需使用句点。由于我们在 import 语句中显式地导入了函数 make_pizza(),因此调用它时只需指定其名称。

6.3. 使用 as 给函数指定别名

如果要导入的函数的名称可能与程序中现有的名称冲突,或者函数的名称太长,可指定简短而独一无二的别名 - 函数的另一个名称,类似于外号。要给函数指定这种特殊外号,需要在导入它时这样做。

下面给函数 make_pizza() 指定了别名 mp()。这是在 import 语句中使用 make_pizza as mp 实现的,关键字 as 将函数重命名为你提供的别名:

from pizza import make_pizza as mp
mp(16, 'pepperoni')
mp(12, 'mushrooms', 'green peppers', 'extra cheese')

上面的 import 语句将函数 make_pizza() 重命名为 mp();在这个程序中,每当需要调用 make_pizza() 时,都可简写成 mp(),而 Python 将运行 make_pizza() 中的代码,这可避免与这个程序可能包含的函数 make_pizza() 混淆。
指定别名的通用语法如下:

from module_name import function_name as fn

6.4. 使用 as 给模块指定别名

你还可以给模块指定别名。通过给模块指定简短的别名 (如给模块 pizza 指定别名 p),让你能够更轻松地调用模块中的函数。相比于 pizza.make_pizza()p.make_pizza() 更为简洁:

import pizza as p
p.make_pizza(16, 'pepperoni')
p.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

上述 import 语句给模块 pizza 指定了别名 p,但该模块中所有函数的名称都没变。调用函数 make_pizza() 时,可编写代码 p.make_pizza() 而不是 pizza.make_pizza(),这样不仅能使代码更简洁,还可以让你不再关注模块名,而专注于描述性的函数名。这些函数名明确地指出了函数的功能,对理解代码而言,它们比模块名更重要。

给模块指定别名的通用语法如下:

import module_name as mn

6.5. 导入模块中的所有函数

使用星号 (*) 运算符可让 Python 导入模块中的所有函数:

from pizza import *
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

import 语句中的星号让 Python 将模块 pizza 中的每个函数都复制到这个程序文件中。由于导入了每个函数,可通过名称来调用每个函数,而无需使用句点表示法。然而,使用并非自己编写的大型模块时,最好不要采用这种导入方法:如果模块中有函数的名称与你的项目中使用的名称相同,可能导致意想不到的结果:Python 可能遇到多个名称相同的函数或变量,进而覆盖函数,而不是分别导入所有的函数。

最佳的做法是,要么只导入你需要使用的函数,要么导入整个模块并使用句点表示法。这能让代码更清晰,更容易阅读和理解。这里之所以介绍这种导入方法,只是想让你在阅读别人编写的代码时,如果遇到类似于下面的 import 语句,能够理解它们:

from module_name import *

7. 函数编写指南

应给函数指定描述性名称,且只在其中使用小写字母和下划线。描述性名称可帮助你和别人明白代码想要做什么。给模块命名时也应遵循上述约定。

每个函数都应包含简要地阐述其功能的注释,该注释应紧跟在函数定义后面,并采用文档字符串格式。文档良好的函数让其他程序员只需阅读文档字符串中的描述就能够使用它:他们完全可以相信代码如描述的那样运行;只要知道函数的名称、需要的实参以及返回值的类型,就能在自己的程序中使用它。

给形参指定默认值时,等号两边不要有空格:

def function_name(parameter_0, parameter_1='default value')

对于函数调用中的关键字实参,也应遵循这种约定:

function_name(value_0, parameter_1='value')

PEP 8 https://www.python.org/dev/peps/pep-0008/ 建议代码行的长度不要超过 79 字符,这样只要编辑器窗口适中,就能看到整行代码。如果形参很多,导致函数定义的长度超过了 79 字符,可在函数定义中输入左括号后按回车键,并在下一行按两次 Tab 键,从而将形参列表和只缩进一层的函数体区分开来。

大多数编辑器都会自动对齐后续参数列表行,使其缩进程度与你给第一个参数列表行指定的缩进程度相同:

def function_name(
		parameter_0, parameter_1, parameter_2,
		parameter_3, parameter_4, parameter_5):
	function body...
# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# Add 4 spaces (an extra level of indentation) to distinguish arguments from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

# Hanging indents should add a level.
foo = long_function_name(
    var_one, var_two,
    var_three, var_four)

如果程序或模块包含多个函数,可使用两个空行将相邻的函数分开,这样将更容易知道前一个函数在什么地方结束,下一个函数从什么地方开始。

所有的 import 语句都应放在文件开头,唯一例外的情形是,在文件开头使用了注释来描述整个程序。

8. 小结

将函数存储在被称为模块的独立文件中,让程序文件更简单、更易于理解。程序员的目标之一是,编写简单的代码来完成任务,而函数有助于你实现这样的目标。它们让你编写好代码块并确定其能够正确运行后,就可置之不理。确定函数能够正确地完成其工作后,你就可以接着投身于下一个编码任务。

函数让你编写代码一次后,想重用它们多少次就重用多少次。需要运行函数中的代码时,只需编写一行函数调用代码,就可让函数完成其工作。需要修改函数的行为时,只需修改一个代码块,而所做的修改将影响调用这个函数的每个地方。
类将函数和数据整洁地封装起来,让你能够灵活而高效地使用它们。

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] (美) Eric Matthes (埃里克•马瑟斯) 著, 袁国忠 译. Python 编程:从入门到实践[M]. 北京:人民邮电出版社, 2016. 1-459

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值