
函数是 Python 编程的核心。它们使您能够有效地构建代码,使其可重用、可维护且更易于调试。
1. 函数介绍函数是可重用的代码块,旨在执行特定任务。可以将 logic 封装到函数中,而不是重复代码,从而提高代码的模块化和可读性。例如,在 AI 项目中,可以编写一个函数来规范化数据或计算精度和召回率等指标。
下面是一个简单的示例:
def greet(name): return f"Hello, {name}!"print(greet("Ebrahim"))# Hello, Ebrahim!函数包括以下内容:
名称:您调用函数时使用的标识符。参数:函数可以接受的输入(可选)。Body:调用函数时运行的代码。返回值:函数的输出(可选)。2. 定义函数函数是使用 def 关键字定义的。您必须提供名称、括号(用于参数)和冒号才能开始正文。
例:def add(a, b): """This function adds two numbers.""" return a + bresult = add(5, 3)print("Sum:", result) # Output: Sum: 8要点:文档字符串(用三引号括起来)记录了函数的用途。return 语句提供输出。如果没有它,该函数将返回 None。缩进是强制性的,它定义了函数的主体。3. 函数参数和参数参数和参数在 Python 中,parameters 是函数定义中列出的变量,而 argument 是调用函数时发送到函数的值。这种区别对于理解函数的运作方式至关重要。
例如,定义另一个计算矩形面积的函数。它需要两个参数:length 和 width。
def calculate_area(length, width): return length * width这里,length 和 width 是 calculate_area 函数的参数。调用此函数时,您可以为这些参数提供特定值 (参数):
area = calculate_area(5, 3) print(area) # Outputs: 15在这种情况下,当我们调用 calculate_area(5, 3) 时,参数 5 和 3 将分别传递给参数 length 和 width,从而计算矩形的面积。
不同类型的参数语法一、 位置参数:
直接在函数中定义。传递的值必须按顺序匹配。
def multiply(x, y): return x * y # Calling the function result = multiply(3, 5) # x=3, y=5 print(result) # Output: 15二、默认参数:
允许您设置参数的默认值。如果未提供参数,则使用默认值。
def greet(name, greeting="Hello"): print(f"{greeting}, {name}!") # Using default parameter greet("Ebrahim") # Output: Hello, Ebrahim! greet("Ela", "Welcome") # Output: Welcome, Ela!三、关键词参数:
可以在调用函数时指定参数的名称,从而允许按任意顺序传递它们。
def display_info(name, age): print(f"Name: {name}, Age: {age}") # Calling with keyword arguments display_info(age=34, name="Ebi") # Order does not matterIV. 可变长度参数:使用 *args 允许函数接受任意数量的位置参数,使用 **kwargs 表示任意数量的关键字参数。
使用 *args:def sum_numbers(*args): return sum(args) # Calling with multiple arguments total = sum_numbers(1, 2, 3, 4, 5) # Output: 15使用 **kwargs:def print_student_info(**kwargs): for key, value in kwargs.items(): print(f"{key}: {value}") # Calling with variable keyword arguments print_student_info(name="Ebi", age=34, course="AI") # Output: # name: Ebi # age: 34 # course: AIV. 组合不同类型的:可以将位置参数、默认参数、*args、关键字参数和 **kwargs 组合到一个函数中。
def mixed_arguments(a, b=2, *args, c=4, **kwargs): print(f"a: {a}, b: {b}, args: {args}, c: {c}, kwargs: {kwargs}") # Example call mixed_arguments(1, 3, 5, 6, c=7, name="Ebi") # Output: # a: 1, b: 3, args: (5, 6), c: 7, kwargs: {'name': 'Ebi'}不同类型的参数语法I. 位置参数:
按定义参数的顺序传递。该数字必须与参数的数量匹配。
def divide(x, y): return x / y # Positional arguments result = divide(10, 2) # x=10, y=2 print(result) # Output: 5.0II. 关键字参数:允许在函数调用期间指定参数名称。这有助于清晰起见,并允许更改顺序。
def display_info(name, age): print(f"Name: {name}, Age: {age}") # Keyword arguments display_info(age=30, name="Ervin") # Output: Name: Ervin, Age: 30III. 默认参数:当没有为具有 default 的参数提供值时自动采用的值。
def introduce(name, country="Iran"): print(f"Hello, my name is {name} and I’m from {country}.") # Calling with and without default introduce("Ebi") # Output: Hello, my name is Ebi and I’m from Iran. introduce("Caterine", "Canada") # Output: Hello, my name is Caterine and I’m from Canada.IV. 可变长度参数:可以使用 *args 作为位置参数,使用 **kwargs 作为关键字参数来传递不同数量的参数。
使用 *args:def concatenate(*args): return ' '.join(args) # Variable-length positional arguments result = concatenate("Hello", "world", "from", "Medium") print(result) # Output: Hello world from Medium使用 **kwargs:def print_game_info(**kwargs): for key, value in kwargs.items(): print(f"{key}: {value}") # Variable-length keyword arguments print_game_info(title="Chess", players=2, duration="30 min") # Output: # title: Chess # players: 2 # duration: 30 minV. 组合参数:可以在一个函数调用中混合使用位置参数、关键字参数和可变长度参数。
def complete_info(a, b=2, *args, c=10, **kwargs): print(f"a: {a}, b: {b}, args: {args}, c: {c}, kwargs: {kwargs}") # Example call complete_info(1, 5, 8, c=3, name="Ebi", age=34) # Output: # a: 1, b: 5, args: (8,), c: 3, kwargs: {'name': 'Ebi', 'age': 34}4. 函数中的范围什么是范围?Scope 是指可以访问变量的程序区域。Python 具有不同的范围:
Local Scope:在函数内部定义的变量,只能在该函数内访问。Global Scope:在所有函数之外定义的变量,可在整个程序中访问。Enclosing Scope:嵌套函数 (closure) 的 enclosing 函数中的变量。Built-in Scope:在 Python 中预先分配的名称(例如,print、len)。了解范围Local Scope:函数中定义的变量位于 local 范围内。它们只能从该函数中访问。例如:def local_scope_example(): x = 10 # x is a local variable print(x) local_scope_example() # Outputs: 10 # print(x) # Uncommenting this line would raise a NameError2. 全局范围:在任何函数外部定义的变量都位于全局范围内,并且可以从代码中的任何位置(包括函数内部)访问,除非被局部变量遮蔽。例如:
y = 20 # y is a global variable def global_scope_example(): print(y) # Accessing global variable y global_scope_example() # Outputs: 20修改全局变量但是,如果要修改函数内的全局变量,则需要将其声明为全局变量:
counter = 0def increment(): global counter counter += 1increment()print(counter) # Output: 1不同范围的示例:x = "global variable"def outer_function(): x = "enclosing variable" def inner_function(): x = "local variable" print(x) # Prints "local variable" inner_function() print(x) # Prints "enclosing variable"outer_function()print(x) # Prints "global variable"最佳实践:避免过度使用全局变量,以保持函数纯度并防止意外的副作用。
5. 函数注释注释提供有关函数的预期输入类型和返回类型的提示。
def add(a: int, b: int) -> int: """Adds two integers.""" return a + bprint(add(3, 4)) # Output: 7注释不强制类型,而是用作文档和支持工具,如类型检查器和 IDE。
6. Lambda 函数Lambda 函数是简洁的匿名函数。它们非常适合排序或筛选等短操作。
square = lambda x: x ** 2print(square(5)) # Output: 25numbers = [1, 2, 3, 4]squared_numbers = list(map(lambda x: x ** 2, numbers))print(squared_numbers) # Output: [1, 4, 9, 16]7. 闭包和嵌套函数嵌套函数另一个函数内的函数称为嵌套函数。
def outer_function(msg): def inner_function(): return f"Inner says: {msg}" return inner_function()print(outer_function("Hello!"))闭 包闭包从其封闭范围内捕获变量,即使在封闭函数完成执行之后也是如此。
def multiplier(factor): def multiply_by(number): return number * factor return multiply_bydouble = multiplier(2)print(double(5)) # Output: 108. 装饰器装饰器是修改或扩展另一个函数行为的高阶函数。
def logger(func): """Logs the function call.""" def wrapper(*args, **kwargs): print(f"Calling {func.__name__} with {args} and {kwargs}") return func(*args, **kwargs) return wrapper@loggerdef multiply(a, b): return a * bprint(multiply(3, 5)) # Logs the call and outputs: 15