函数, 类, 模块, 包第一篇:函数内容笔记

@

函数篇

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

  • 优秀的函数通用性高
  • 定义在独立模块的函数, 让程序文件更简单, 更易于理解
  • 遵循函数编辑指南可以让程序始终结构良好, 更易于阅读

每一个函数都应只负责一项具体的工作, 有利于程序的结构

定义函数

# greet.py
def greet_user():  # 函数定义, 向Python指出函数名
    """显示简单的问候语"""  # 文档字符串 docstring
    print("Hello !")  # 函数体
    
greet_user()  # 调用函数
Hello !

向函数传递信息

def greet_user(username):
    """显示简单的问候语"""
    print("Hello, " + username.title() + '!')
    
greet_user('jesse')
Hello, Jesse!

实参和形参

username => 形参
'jesse' => 实参
这里,可以根据实际需求调用函数greet_user()任意次

传递参数

位置实参

  • 位置实参 要求实参的顺序和形参的顺序相同
# pets.py
def describe_pet(animal_type, pet_name):
    """显示宠物的信息"""
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    
describe_pet('hamster', 'harry')  # 指定位置

describe_pet('willie', 'dog')  # 指定位置, 实参位置错误,输出结果就笑话了

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

I have a willie.
My willie's name is Dog.

关键字实参

  • 传递给函数的名称-值对
# pets.py重写
def describe_pet(animal_type, pet_name):
    """显示宠物的信息"""
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    
describe_pet(animal_type='hamster', pet_name='harry')  # 关键字实参调用
describe_pet(pet_name='harry', animal_type='hamster')  # 等效于上一行函数调用
I have a hamster.
My hamster's name is Harry.

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

默认值

# pets.py
def describe_pet(pet_name, animal_type='dog'):  # 形参等号左右两侧不允许空格
    """显示宠物的信息"""
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    
describe_pet(pet_name='willie')  # 指定位置, 实参位置错误,输出结果就笑话了

I have a dog.
My dog's name is Willie.
# 更简洁的方式
describe_pet('willie')
I have a dog.
My dog's name is Willie.
# 替换形参默认值
describe_pet(pet_name='harry', animal_type='hamster')
I have a hamster.
My hamster's name is Harry.

等效的函数调用

  • 可以混合使用位置实参, 关键字实参和默认值
# pets-1.py
def describe_pets(pet_name, animal_type='dog'):  # 形参等号左右两侧不允许空格
    """显示宠物的信息"""
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    
# 一条名为Willie的小狗
describe_pet('willie')
describe_pet(pet_name='willie')

# 一只名为Harry的仓鼠
describe_pet('harry', 'hamster')
describe_pet(animal_type='hamster', pet_name='harry')
describe_pet(pet_name='harry', animal_type='hamster')
I have a dog.
My dog's name is Willie.

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

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

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

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

使用时要避免实参错误

describe_pets()  # 实参未传入
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-12-56e72078aad7> in <module>()
----> 1 describe_pets()  # 实参未传入


TypeError: describe_pets() missing 1 required positional argument: 'pet_name'

返回值

  • return语句可以将值返回到调用函数的代码行, 默认值为None

返回简单值

# formatted_name.py
def get_formatted_name(first_name, last_name):
    """返回整洁的姓名"""
    full_name = first_name + " " + last_name
    return full_name.title()

musician = get_formatted_name('jimi', 'hendrix')
print(musician)
Jimi Hendrix

实参可选的情况

# formatted_name.py
def get_formatted_name(first_name, middle_name, last_name):
    """返回整洁的姓名"""
    full_name = first_name + " " + middle_name + " " + last_name
    return full_name.title()

musician = get_formatted_name('john', 'lee', 'hendrix')
print(musician)
John Lee Hendrix

使用if/else 进行选择

# formatted_name.py
def get_formatted_name(first_name, last_name, middle_name=''):  # 可选的参数要放在最右边
    """返回整洁的姓名"""
    if middle_name:
        full_name = first_name + " " + middle_name + " " + last_name
    else:
        full_name = first_name + " " + last_name
    return full_name.title()

musician = get_formatted_name('jimi', 'hendrix')
print(musician)

musician = get_formatted_name('john', 'hooker', 'lee')
print(musician)
Jimi Hendrix
John Lee Hooker

返回字典

  • 函数可以返回任何类型的值, 包括字典,列表等较复杂的数据结构
# person.py
def build_person(first_name, last_name):
    """返回一个字典, 其中包含有关于一个人的信息"""
    person = {'first': first_name, 'last': last_name}
    return person

musician = build_person('jimi', 'hendrix')
print(musician)
{'first': 'jimi', 'last': 'hendrix'}

扩展简单的文本信息

  • 可选形参age, 设置默认值为空字符串
# person.py
def build_person(first_name, last_name, age=' '):
    """返回一个字典, 其中包含有关于一个人的信息"""
    person = {'first': first_name, 'last': last_name}
    if age:
        person['age'] = age
    return person

musician = build_person('jimi', 'hendrix', age=27)
print(musician)
{'first': 'jimi', 'last': 'hendrix', 'age': 27}

结合使用函数和while循环

# formatted_name.py
def get_formatted_name(first_name, last_name):
    """返回整洁的姓名"""
    full_name = first_name + " " + last_name
    return full_name.title()

# 这是一个无限循环
while True:
    print("\nPlease tell me your name: ")
    f_name = input("First name: ")
    l_name = input("Last name: ")
    
    formatted_name = get_formatted_name(f_name, l_name)
    print("\nHello, " + formatted_name + "!")
Please tell me your name: 
First name: li
Last name: lei

Hello, Li Lei!

Please tell me your name: 

使用break退出循环的简单途径

# formatted_name.py
def get_formatted_name(first_name, last_name):
    """返回整洁的姓名"""
    full_name = first_name + " " + last_name
    return full_name.title()

# 这是一个无限循环
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 + "!")
Please tell me your name: 
enter 'q' at any time to quit
First name: eric
Last name: matthes

Hello, Eric Matthes!

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

传递列表

# greet_users.py
def greet_users(names):
    """向列表中的每一位用户都发出简单的问候"""
    for name in names:
        msg = "Hello, " + name.title() + "!"
        print(msg)

# 传递一个列表
usernames = ['lilei', 'hmm', 'margot']
greet_users(usernames)
Hello, Lilei!
Hello, Hmm!
Hello, Margot!

模拟函数调用函数

  • 第一个函数负责打印设计的工作
  • 第二个函数负责打印了哪些设计
  • 在一个函数中调用另一个函数有助于将复杂的任务划分为一系列的步骤
# printing_models.py
def print_models(unprinted_designs, completed_models):
    """
    模拟打印每个设计, 直到没有未打印的设计为止
    打印每个设计后, 都将其移动到列表complete_models中
    """
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        
        # 模拟根据设计制作3D打印模型的过程
        print("Printing model: " + current_design)
        completed_models.append(current_design)

def show_completed_models(completed_models):
    """显示打印好的所有模型"""
    print("\nThe following models have been printed: ")
    for completed_model in completed_models:
        print(completed_model)
        
unprinted_designs = [ 'iphone case', 'robot pendant', 'dodecahedron']  # print_models传入一个列表
completed_models = []  # 创建一个空列表给 show_completed_models

print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
Printing model: dodecahedron
Printing model: robot pendant
Printing model: iphone case

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

禁用函数修改列表

  • 有时候为了不修改列表本身,可以使用列表的切片
    function_name(list_name[:])
print_models(unprinted_designs[:], completed_models)
show_completed_models(completed_models)
The following models have been printed: 
dodecahedron
robot pendant
iphone case

这里处理的处理的意义在于让函数使用现成列表可以避免花时间和内存创建副本,从而提高效率, 在处理大型列表时就可以提现优势

传递任意数量的实参

  • *args **kwargs: args ->tuple, kwargs ->dict
# pizza.py
def make_pizza(*args):
    """打印顾客点餐的所有配料"""
    print(args)
    
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
('pepperoni',)
('mushrooms', 'green peppers', 'extra cheese')

使用print语句替换成一个循环

# pizza.py
def make_pizza(*args):
    """概述要制作的比萨"""
    print("\nMaking a pizza with the following toppings: ")
    for topping in args:
        print('- ' + topping)
    
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
Making a pizza with the following toppings: 
- pepperoni

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

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

# pizza.py
def make_pizza(size, *args):
    """概述要制作的比萨"""
    print("\nMaking a " + str(size) +
          "-inch pizza with the following toppings: ")
    for topping in args:
        print('- ' + topping)
    
make_pizza(16, 'pepperoni')
make_pizza(13, 'mushrooms', 'green peppers', 'extra cheese')
Making a 16-inch pizza with the following toppings: 
- pepperoni

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

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

# user_profile.py
def build_profile(first, last,  **user_info):
    """创建一个字典, 其中包含我们知道的有关于用户的一切"""
    profile = {}
    profile["first_name"] = first
    profile["last_name"] = last
    for key, val in user_info.items():
        profile[key] = val
    return profile

user_profile = build_profile("albert", 'einstein',
                            location='princeton',
                            field='physics')
print(user_profile)
{'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}

将函数存储在模块中

  • 函数的优点之一是, 使用它们可以将代码块和主程序分离. 通过给函数指定描述性名称, 可以让主程序容易理解的多(不要使用中文拼音来写, 这样很low)
  • 函数可以储存在被称为模块的独立文件中(py文件), 再将模块导入到主程序,使用import来允许当前运行的程序文件中可以使用模块中的代码块

导入模块的方法如下

导入整个模块(两个模块要在同一个目录文件夹下)

import 函数名

导入特定的函数

import module_name import function_name

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

  • from module_name import function_0, function1, ....

使用as给函数指定别名

  • 当一行代码语句超出79时, 为了符合pep8规范

from pizza import make_pizza as mp

mp(16, 'pepperoni')
mp(13, 'mushrooms', 'green peppers', 'extra cheese')

使用*导入模块的所有函数

from pizza import *

模块搜索路径方式:

未完, 待续....

<Python编程 从入门到实践>关于函数的笔记

posted @ 2019-02-26 00:14  lataku~~  阅读(133)  评论(0编辑  收藏  举报