python函数介绍

参数

1. 关键字实参

通过关键字实参,函数可以不关心传参的顺序,代码如下:

先说一个反例,不用关键字传参,由于参数顺序的不同,导致传参错误。

#//不带关键字实参的函数,需要注意传参的顺序
def describe_pet1(animal_type, pet_name):
    print('My ' + animal_type + ' is ' + pet_name)
describe_pet1('dog', 'Tony')

#//下面是一个错误的例子
def describe_pet2(animal_type, pet_name):
    print('My ' + animal_type + ' is ' + pet_name)
describe_pet2('Tony', 'dog')

由于顺序不同,结果输出

My dog is Tony
My Tony is dog

下面这个是正确的案例

#//关键字实参
def describe_pet2(animal_type, pet_name):
    print('My ' + animal_type + ' is ' + pet_name)
describe_pet2(animal_type = 'dog', pet_name = 'Tony')

由于传递的是关键字参数,所以结果为

My dog is Tony

2. 默认值

编写函数时,可以给参数设定默认值,调用函数的时候,就可以不用传递该参数。
传递默认值
注意:带默认值的参数要放在不带默认值的参数后面,否则会报错

def describe_pet3(pet_name, animal_type = 'dog'):
    print('My ' + animal_type + ' is ' + pet_name)
describe_pet2(pet_name = 'Tony')

输出结果为

My dog is Tony

3. 在函数参数中传递列表

模仿打印机工作的案例

#//在函数中嵌套while循环,模拟打印机的未打印列表和已打印列表
def print_models(unprinted_models, completed_models):

    #//用while循环,打印所有文件
    while unprinted_models:
        current_model = unprinted_models.pop()

        #//模拟打印过程
        print('print model: ' + current_model)

        #//放入已打印列表
        completed_models.append(current_model)

#//已完成列表展示函数
def show_completed_models(completed_models):
    print('\nThe following models have been printed:')
    for completed_model in completed_models:
        print(completed_model)
unprinted_models = ['file1', 'file2', 'file3']
completed_models = []

print_models(unprinted_models, completed_models)
#//在上面的函数中,completed_models被永久改变了,这个是重点
show_completed_models(completed_models)

输出的结果为:

print model: file3
print model: file2
print model: file1

The following models have been printed:
file3
file2
file1

4. 传递任意数量的参数

在函数的形参前,加入*,参数变为可变长类型

展示动物园中的动物的案例

#//传递任意数量的实参的函数
def animalInZoo(*animals):
    #//可以直接打印
    print(animals)

def animalInAnotherZoo(*animals):
    print('animalInAnotherZoo()')
    #//可以用for循环的方式打印可变参数
    for animal in animals:
        print(animal)

# //调用函数
animalInZoo('dog', 'cat', 'wolf')
animalInAnotherZoo('dog', 'rat', 'tiger')

输出结果如下:

('dog', 'cat', 'wolf')
animalInAnotherZoo()
dog
rat
tiger

传递任意数量的关键字参数

在函数中,可以使用**,来传递任意数量的关键字参数,代码如下

#//传递任意数量的关键字实参(用**),参数传进来的是key-value对,最好用字典接受
def user_info(first, last, **info):
    #创建一个字典,用于接受用户所有信息
    user_info_dict = {}
    #//把定长参数传进来
    user_info_dict['first_name'] = first
    user_info_dict['last_name'] = last
    #//把不定长参数传进来
    for key, value in info.items():
        user_info_dict[key] = value
    #//返回字典
    return user_info_dict

#//调用函数,并接收返回值
result_dict = user_info('Ryan', 'Qing',
                        sex = 'male',
                        intrest = 'reading',
                        age = '29')
print(result_dict)

输出结果如下:

{'first_name': 'Ryan', 'last_name': 'Qing', 'sex': 'male', 'intrest': 'reading', 'age': '29'}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第5章 函数和函数式编程 5.1 引言函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。函数...
    VIVAFT阅读 999评论 0 5
  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 2,896评论 2 9
  • 函数和对象 1、函数 1.1 函数概述 函数对于任何一门语言来说都是核心的概念。通过函数可以封装任意多条语句,而且...
    道无虚阅读 4,679评论 0 5
  • 1. 定义函数 1.1 一般函数 函数是带名字的代码块,该代码块是完成特定工作的固定代码序列。如果程序中多次出现相...
    小天真_5eeb阅读 520评论 0 2
  • 函数 函数是执行特定任务的自包含代码块。给定函数一个名称作为标识,并在需要的时候通过调用其名称来执行任务。 Swi...
    BoyceC阅读 449评论 0 1