一键掌握:Python 函数声明与调用的 20 个优秀实践
2024-06-25 17:15:48 软件 126观看
摘要今天我们要一起探索的是Python世界中的一块基石——函数!想象一下,像魔术师一样,轻轻一挥手,复杂的任务就被封装成简洁的命令,这就是函数的魅力。下面,让我们用最接地气的方式,揭开它的神秘面纱,掌握那些让代码飞起来的20个小

今天我们要一起探索的是Python世界中的一块基石——函数!想象一下,像魔术师一样,轻轻一挥手,复杂的任务就被封装成简洁的命令,这就是函数的魅力。下面,让我们用最接地气的方式,揭开它的神秘面纱,掌握那些让代码飞起来的20个小技巧。HjH28资讯网——每日最新资讯28at.com

HjH28资讯网——每日最新资讯28at.com

1. 基础中的基础:Hello, Function!

def say_hello(name="World"):    print(f"Hello, {name}!")say_hello("Pythonista")  # 输出: Hello, Pythonista!

解密:def是定义函数的关键词,say_hello是函数名,括号内是参数,如果没有提供参数,就用默认值。HjH28资讯网——每日最新资讯28at.com

2. 参数传递:位置VS关键字

def greet(firstName, lastName):    print(f"Hi, I'm {firstName} {lastName}")greet(lastName="Smith", firstName="John")  # 明确指定参数名

小贴士:通过名字指定参数,让代码更易读,特别是参数多时。HjH28资讯网——每日最新资讯28at.com

3. *args 和 **kwargs:无限参数的秘密

def super_greet(*names):  # *args 收集位置参数    for name in names:        print(f"Hello, {name}!")    super_greet("Alice", "Bob", "Charlie")  # 多个名字一次性处理def versatile_greet(**details):  # **kwargs 收集关键字参数    for key, value in details.items():        print(f"{key.title()}: {value}")versatile_greet(age=25, city="New York")  # 关键信息一网打尽

神奇之处:*args和**kwargs让你的函数可以接受任意数量的参数,超级灵活!HjH28资讯网——每日最新资讯28at.com

4. 返回值不只是一个

def multiple_returns():    return "Success", 200result, status = multiple_returns()print(result, status)  # 输出: Success 200

多才多艺:函数可以返回多个值,其实是以元组的形式返回的。HjH28资讯网——每日最新资讯28at.com

5. 文档字符串:让代码会说话

def calculate_area(radius):    """    计算圆的面积。        参数:    radius (float): 圆的半径        返回:    float: 圆的面积    """    import math    return math.pi * radius**2print(calculate_area.__doc__)  # 查看文档字符串

文明交流:良好的文档字符串是团队合作的润滑剂,也是自我复习的好帮手。HjH28资讯网——每日最新资讯28at.com

6. 默认参数的坑

def append_to_list(item, my_list=[]):    my_list.append(item)    return my_listprint(append_to_list(1))  # [1]print(append_to_list(2))  # 注意!这里会是 [1, 2],不是预期的 [2]

警告:默认参数在函数定义时就初始化了,多次调用时会保留之前的值,小心这个陷阱。HjH28资讯网——每日最新资讯28at.com

7. 变量作用域:谁能访问我?

x = "global"def scope_test():    x = "local"    print(x)  # localscope_test()print(x)  # global

名字游戏:在函数内部定义的变量默认是局部的,不会影响到外部的同名变量。HjH28资讯网——每日最新资讯28at.com

8. 非局部变量的修改

y = 10def modify_outer():    global y  # 告诉Python你想修改外部的y    y = 20modify_outer()print(y)  # 输出: 20

特权操作:使用global关键字可以让函数内部修改全局变量,但要谨慎使用。HjH28资讯网——每日最新资讯28at.com

9. 闭包:函数内的函数

def counter():    count = 0    def increment():        nonlocal count        count += 1        return count    return incrementmy_counter = counter()print(my_counter())  # 1print(my_counter())  # 2

内外有别:闭包允许内部函数访问并修改外部函数的变量,而外部函数返回的是内部函数的引用。HjH28资讯网——每日最新资讯28at.com

10. 装饰器:给函数穿上花衣

def my_decorator(func):    def wrapper():        print("Something is happening before the function is called.")        func()        print("Something is happening after the function is called.")    return wrapper@my_decoratordef say_hello():    print("Hello!")say_hello()

装饰生活,装饰函数:装饰器是Python的一大特色,它可以在不修改原函数代码的情况下增加新功能。HjH28资讯网——每日最新资讯28at.com

高级使用场景

11. 递归:自己调用自己的艺术

def factorial(n):    if n == 1:        return 1    else:        return n * factorial(n-1)print(factorial(5))  # 输出: 120

无限循环的智慧:递归是解决某些问题的强大工具,但要注意避免无限循环,确保有一个清晰的终止条件。HjH28资讯网——每日最新资讯28at.com

12. 匿名函数lambda:简洁之美

double = lambda x: x * 2print(double(5))  # 输出: 10squared = lambda x: x**2numbers = [1, 2, 3]print(list(map(squared, numbers)))  # 输出: [1, 4, 9]

一闪即逝的美:lambda函数适合简单的操作,它们无需定义即可使用,非常适合用在高阶函数中。HjH28资讯网——每日最新资讯28at.com

13. map()函数:批量操作的艺术

def square(n):    return n*nnumbers = [1, 2, 3, 4]squared_numbers = list(map(square, numbers))print(squared_numbers)  # 输出: [1, 4, 9, 16]# 或者用lambda简化simplified = list(map(lambda x: x*x, numbers))print(simplified)  # 同上

批量处理好帮手:map函数对序列的每个元素应用指定函数,返回一个迭代器对象,通常转换为列表使用。HjH28资讯网——每日最新资讯28at.com

14. filter()函数:筛选高手

def is_even(n):    return n % 2 == 0numbers = [1, 2, 3, 4, 5, 6]even_numbers = list(filter(is_even, numbers))print(even_numbers)  # 输出: [2, 4, 6]# 简化版even_with_lambda = list(filter(lambda x: x % 2 == 0, numbers))print(even_with_lambda)  # 同上

只选对的:filter函数根据提供的函数来筛选序列中的元素,返回一个迭代器,同样常用list转换。HjH28资讯网——每日最新资讯28at.com

15. reduce()函数:累积计算的秘密武器

from functools import reducedef accumulator(acc, item):    return acc + itemnumbers = [1, 2, 3, 4]sum_of_numbers = reduce(accumulator, numbers, 0)print(sum_of_numbers)  # 输出: 10# 或用lambda简化sum_with_lambda = reduce(lambda acc, item: acc + item, numbers, 0)print(sum_with_lambda)  # 同上

累积力量:reduce将一个函数应用于序列的所有元素,累积结果,非常适合求和、乘积等操作。HjH28资讯网——每日最新资讯28at.com

16. 偏函数partial:定制化的便捷

from functools import partialdef power(base, exponent):    return base ** exponentsquare = partial(power, exponent=2)print(square(5))  # 输出: 25cube = partial(power, exponent=3)print(cube(3))  # 输出: 27

定制你的函数:偏函数可以固定原函数的部分参数,生成新的函数,非常适用于需要多次调用且参数变化不大的场景。HjH28资讯网——每日最新资讯28at.com

17. 递归优化与尾递归

# 注意:Python标准解释器不直接支持尾递归优化def factorial_tail(n, accumulator=1):    if n == 1:        return accumulator    else:        return factorial_tail(n-1, n*accumulator)print(factorial_tail(5))  # 输出: 120

尾声:虽然Python没有内置的尾递归优化,理解尾递归的概念对理解函数调用栈很有帮助。HjH28资讯网——每日最新资讯28at.com

18. 闭包进阶:数据封装

def counter_maker():    count = 0    def increment():        nonlocal count        count += 1        return count    return incrementcounter1 = counter_maker()counter2 = counter_maker()print(counter1(), counter1())  # 输出: 1 2print(counter2(), counter2())  # 输出: 1 2

工厂模式:闭包可以用来创建具有独立状态的函数,类似于面向对象中的实例。HjH28资讯网——每日最新资讯28at.com

19. 高阶函数:函数的函数

def apply_operation(func, a, b):    return func(a, b)add = lambda x, y: x + ysubtract = lambda x, y: x - yprint(apply_operation(add, 5, 3))  # 输出: 8print(apply_operation(subtract, 5, 3))  # 输出: 2

函数的魔力:高阶函数可以接受函数作为参数或返回函数,这是函数式编程的核心概念。HjH28资讯网——每日最新资讯28at.com

20. 装饰器进阶:带参数的装饰器

def repeat(n):    def decorator(func):        def wrapper(*args, **kwargs):            for _ in range(n):                func(*args, **kwargs)        return wrapper    return decorator@repeat(3)def say_hello():    print("Hello!")say_hello()  # 输出: Hello! Hello! Hello!

装饰器的新维度:带参数的装饰器让装饰器本身也变得灵活,可以根据需要调整行为。HjH28资讯网——每日最新资讯28at.com

至此,我们探索了Python函数从基础到进阶的20个最佳实践,每一个点都是打开新视野的钥匙。HjH28资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-96421-0.html一键掌握:Python 函数声明与调用的 20 个优秀实践

声明:本网页内容旨在传播知识,不代表本站观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。

显示全文

上一篇:LangChain转换链:让数据处理更精准

下一篇:​英特尔李映:以技术与专长赋能开发者,共同推动开源技术的蓬勃发展

最新热点