PYTHON基础技能 – 设计Python函数参数的19个高级指南

释放双眼,带上耳机,听听看~!

今天我们将深入探讨如何设计Python函数的参数,让你的代码更加灵活、高效和易用。我们会从基础开始,逐步引入更高级的概念和技术,确保你能够全面掌握这些技巧。

  1. 使用默认参数值
    理论知识:默认参数值允许你在调用函数时省略某些参数。这使得函数更加灵活,用户可以根据需要选择是否传递这些参数。

示例代码:

def greet(name, greeting=”Hello”):
return f”{greeting}, {name}!”

调用示例

print(greet(“Alice”)) # 输出: Hello, Alice!
print(greet(“Bob”, “Hi”)) # 输出: Hi, Bob!
代码解释:

greet 函数有两个参数:name 和 greeting。

greeting 参数有一个默认值 “Hello”。

当调用 greet(“Alice”) 时,greeting 使用默认值 “Hello”。

当调用 greet(“Bob”, “Hi”) 时,greeting 使用传入的值 “Hi”。

  1. 可变参数列表
    理论知识:使用 *args 和 **kwargs 可以让函数接受任意数量的位置参数和关键字参数。这使得函数更加通用,适用于多种情况。

示例代码:

def print_args(*args):
for arg in args:
print(arg)

def print_kwargs(**kwargs):
for key, value in kwargs.items():
print(f”{key}: {value}”)

调用示例

print_args(1, 2, 3) # 输出: 1 2 3
print_kwargs(a=1, b=2, c=3) # 输出: a: 1 b: 2 c: 3
代码解释:

print_args 函数使用 *args 接受任意数量的位置参数。

print_kwargs 函数使用 **kwargs 接受任意数量的关键字参数。

*args 和 **kwargs 都是元组和字典的形式,可以在函数内部进行遍历。

  1. 关键字参数
    理论知识:关键字参数允许你在调用函数时指定参数名称,这样可以提高代码的可读性和灵活性。

示例代码:

def describe_pet(animal_type, pet_name):
return f”I have a {animal_type} named {pet_name}.”

调用示例

print(describe_pet(animal_type=”hamster”, pet_name=”Harry”)) # 输出: I have a hamster named Harry.
代码解释:

describe_pet 函数有两个参数:animal_type 和 pet_name。

在调用函数时,使用关键字参数 animal_type=”hamster” 和 pet_name=”Harry”,使代码更易读。

  1. 位置参数和关键字参数混合使用
    理论知识:你可以同时使用位置参数和关键字参数,但位置参数必须在关键字参数之前。

示例代码:

def describe_pet(pet_name, animal_type=”dog”):
return f”I have a {animal_type} named {pet_name}.”

调用示例

print(describe_pet(“Willie”)) # 输出: I have a dog named Willie.
print(describe_pet(“Harry”, animal_type=”hamster”)) # 输出: I have a hamster named Harry.
代码解释:

describe_pet 函数有一个位置参数 pet_name 和一个带有默认值的关键字参数 animal_type。

在调用函数时,位置参数必须在关键字参数之前。

  1. 强制关键字参数
    理论知识:使用 * 可以强制某些参数必须以关键字形式传递,这有助于提高代码的可读性和清晰度。

示例代码:

def describe_pet(pet_name, *, animal_type=”dog”):
return f”I have a {animal_type} named {pet_name}.”

调用示例

print(describe_pet(“Willie”)) # 输出: I have a dog named Willie.

print(describe_pet(“Harry”, “hamster”)) # 报错

print(describe_pet(“Harry”, animal_type=”hamster”)) # 输出: I have a hamster named Harry.
代码解释:

describe_pet 函数中,* 后面的参数 animal_type 必须以关键字形式传递。

如果尝试以位置参数的形式传递 animal_type,会引发错误。

  1. 带有默认值的强制关键字参数
    理论知识:你可以为强制关键字参数设置默认值,这样在调用函数时可以选择是否传递这些参数。

示例代码:

def describe_pet(pet_name, *, animal_type=”dog”, age=None):
description = f”I have a {animal_type} named {pet_name}.”
if age is not None:
description += f” It is {age} years old.”
return description

调用示例

print(describe_pet(“Willie”)) # 输出: I have a dog named Willie.
print(describe_pet(“Harry”, animal_type=”hamster”, age=2)) # 输出: I have a hamster named Harry. It is 2 years old.
代码解释:

describe_pet 函数中有两个强制关键字参数:animal_type 和 age。

age 参数有一个默认值 None,如果未传递 age,则不会包含年龄信息。

  1. 使用类型注解
    理论知识:类型注解可以帮助你更好地理解和维护代码,提高代码的可读性和健壮性。

示例代码:

def add_numbers(a: int, b: int) -> int:
return a + b

调用示例

result = add_numbers(3, 5)
print(result) # 输出: 8
代码解释:

add_numbers 函数的参数 a 和 b 都有类型注解 int。

返回值也有类型注解 int。

类型注解不强制执行类型检查,但可以在开发工具中提供更好的提示和支持。

  1. 使用可选类型注解
    理论知识:使用 Optional 类型注解可以表示某个参数或返回值可能是 None。

示例代码:

from typing import Optional

def greet(name: str, greeting: Optional[str] = None) -> str:
if greeting is None:
greeting = “Hello”
return f”{greeting}, {name}!”

调用示例

print(greet(“Alice”)) # 输出: Hello, Alice!
print(greet(“Bob”, “Hi”)) # 输出: Hi, Bob!
代码解释:

greet 函数的 greeting 参数使用 Optional[str] 类型注解,表示它可以是 str 或 None。

如果 greeting 为 None,则使用默认值 “Hello”。

  1. 使用列表和字典类型注解
    理论知识:使用 List 和 Dict 类型注解可以更精确地描述参数和返回值的类型。

示例代码:

from typing import List, Dict

def process_data(data: List[Dict[str, int]]) -> int:
total = 0
for item in data:
total += item[“value”]
return total

调用示例

data = [{“value”: 10}, {“value”: 20}, {“value”: 30}]
result = process_data(data)
print(result) # 输出: 60
代码解释:

process_data 函数的参数 data 是一个包含字典的列表,每个字典都有一个键 “value”。

返回值是一个整数,表示所有字典中 “value” 键的值之和。

  1. 使用自定义类型注解
    理论知识:你可以定义自己的类型别名,使类型注解更加清晰和简洁。

示例代码:

from typing import List, Dict

定义类型别名

DataItem = Dict[str, int]
DataList = List[DataItem]

def process_data(data: DataList) -> int:
total = 0
for item in data:
total += item[“value”]
return total

调用示例

data = [{“value”: 10}, {“value”: 20}, {“value”: 30}]
result = process_data(data)
print(result) # 输出: 60
代码解释:

DataItem 和 DataList 是自定义的类型别名,分别表示包含整数值的字典和包含这些字典的列表。

使用类型别名可以使代码更易读和维护。

  1. 使用命名元组
    理论知识:命名元组(namedtuple)是一种轻量级的类,可以用来创建具有命名字段的不可变对象。使用命名元组可以提高代码的可读性和结构化。

示例代码:

from collections import namedtuple

定义命名元组

Person = namedtuple(‘Person’, [‘name’, ‘age’])

def greet_person(person: Person) -> str:
return f”Hello, {person.name}! You are {person.age} years old.”

调用示例

alice = Person(name=”Alice”, age=30)
print(greet_person(alice)) # 输出: Hello, Alice! You are 30 years old.
代码解释:

Person 是一个命名元组,有两个字段:name 和 age。

greet_person 函数接受一个 Person 对象作为参数,并使用其字段生成问候语。

  1. 使用数据类
    理论知识:数据类(dataclass)是Python 3.7引入的一个装饰器,用于自动生成特殊方法(如 initrepr),使类的定义更加简洁。

示例代码:

from dataclasses import dataclass

@dataclass
class Person:
name: str
age: int

def greet_person(person: Person) -> str:
return f”Hello, {person.name}! You are {person.age} years old.”

调用示例

alice = Person(name=”Alice”, age=30)
print(greet_person(alice)) # 输出: Hello, Alice! You are 30 years old.
代码解释:

Person 类使用 @dataclass 装饰器,自动生成 initrepr 方法。

greet_person 函数接受一个 Person 对象作为参数,并使用其字段生成问候语。

  1. 使用类型别名和泛型
    理论知识:类型别名和泛型可以进一步提高类型注解的灵活性和可读性。泛型允许你定义可以处理多种类型的函数和类。

示例代码:

from typing import TypeVar, List

T = TypeVar(‘T’)

def get_first_element(lst: List[T]) -> T:
return lst[0]

调用示例

numbers = [1, 2, 3]
fruits = [“apple”, “banana”, “cherry”]

print(get_first_element(numbers)) # 输出: 1
print(get_first_element(fruits)) # 输出: apple
代码解释:

T 是一个类型变量,表示 get_first_element 函数可以接受任何类型的列表。

get_first_element 函数返回列表的第一个元素,类型与列表元素类型相同。

  1. 使用 @staticmethod 和 @classmethod
    理论知识:静态方法(@staticmethod)和类方法(@classmethod)可以让你在类中定义不需要实例化的方法,适用于一些工具函数和工厂方法。

示例代码:

class MathUtils:
@staticmethod
def add(a: int, b: int) -> int:
return a + b


1
2
3
@classmethod
def multiply(cls, a: int, b: int) -> int:
    return a * b

调用示例

print(MathUtils.add(3, 5)) # 输出: 8
print(MathUtils.multiply(3, 5)) # 输出: 15
代码解释:

MathUtils 类有两个方法:add 和 multiply。

add 是一个静态方法,不需要类实例即可调用。

multiply 是一个类方法,可以通过类名调用。

  1. 使用 @property 装饰器
    理论知识:属性(@property)装饰器允许你将方法伪装成属性,提供更自然的访问方式。

示例代码:

class Circle:
def init(self, radius: float):
self.radius = radius


1
2
3
@property
def diameter(self) -> float:
    return 2 * self.radius

调用示例

circle = Circle(5)
print(circle.diameter) # 输出: 10.0
代码解释:

Circle 类有一个 radius 属性和一个 diameter 属性。

diameter 使用 @property 装饰器,可以通过 circle.diameter 访问,而无需调用方法。

  1. 使用 * 分割位置参数和关键字参数
    理论知识:使用 * 可以明确区分位置参数和关键字参数,提高函数的可读性和灵活性。

示例代码:

def describe_pet(pet_name, *, animal_type, age=None):
description = f”I have a {animal_type} named {pet_name}.”
if age is not None:
description += f” It is {age} years old.”
return description

调用示例

print(describe_pet(“Willie”, animal_type=”dog”)) # 输出: I have a dog named Willie.
print(describe_pet(“Harry”, animal_type=”hamster”, age=2)) # 输出: I have a hamster named Harry. It is 2 years old.
代码解释:

describe_pet 函数中,* 后面的参数 animal_type 和 age 必须以关键字形式传递。

这样可以避免位置参数和关键字参数的混淆。

  1. 使用 ** 解包字典
    理论知识:使用 ** 可以将字典解包为关键字参数,方便传递多个参数。

示例代码:

def describe_pet(pet_name, animal_type, age=None):
description = f”I have a {animal_type} named {pet_name}.”
if age is not None:
description += f” It is {age} years old.”
return description

调用示例

pet_info_with_age = {“pet_name”: “Harry”, “animal_type”: “hamster”, “age”: 2}
print(describe_pet(**pet_info_with_age)) # 输出: I have a hamster named Harry. It is 2 years old.
代码解释:

pet_info = {“pet_name”: “Willie”, “animal_type”: “dog”}
print(describe_pet(**pet_info)) # 输出: I have a dog named Willie.

pet_info 和 pet_info_with_age 是包含关键字参数的字典。

使用 ** 将字典解包为关键字参数传递给 describe_pet 函数。

  1. 使用 * 解包列表
    理论知识:使用 * 可以将列表解包为位置参数,方便传递多个参数。

示例代码:

def add_numbers(a, b, c):
return a + b + c

调用示例

numbers = [1, 2, 3]
print(add_numbers(*numbers)) # 输出: 6
代码解释:

numbers 是一个包含三个数字的列表。

使用 * 将列表解包为位置参数传递给 add_numbers 函数。

  1. 使用 functools.partial 创建部分应用函数
    理论知识:functools.partial 可以创建一个部分应用函数,固定某些参数,减少函数调用时的参数数量。

示例代码:

from functools import partial

def power(base, exponent):
return base ** exponent

创建部分应用函数

square = partial(power, exponent=2)
cube = partial(power, exponent=3)

调用示例

print(square(2)) # 输出: 4
print(cube(2)) # 输出: 8
代码解释:

power 函数接受两个参数:base 和 exponent。

使用 partial 创建两个部分应用函数:square 和 cube。

square 固定了 exponent 为 2,cube 固定了 exponent 为 3。

实战案例:日志记录器
假设你正在开发一个应用程序,需要记录不同级别的日志(如调试、信息、警告和错误)。我们可以设计一个灵活的日志记录器函数,支持不同的日志级别和格式化选项。

示例代码:

import logging
from typing import Optional

设置日志格式

logging.basicConfig(level=logging.DEBUG, format=’%(asctime)s – %(levelname)s – %(message)s’)

def log_message(level: str, message: str, extra_info: Optional[str] = None):
if level.lower() == “debug”:
logging.debug(message if extra_info is None else f”{message} – {extra_info}”)
elif level.lower() == “info”:
logging.info(message if extra_info is None else f”{message} – {extra_info}”)
elif level.lower() == “warning”:
logging.warning(message if extra_info is None else f”{message} – {extra_info}”)
elif level.lower() == “error”:
logging.error(message if extra_info is None else f”{message} – {extra_info}”)
else:
raise ValueError(“Invalid log level”)

调用示例

log_message(“debug”, “This is a debug message”)
log_message(“info”, “This is an info message”, “Additional info”)
log_message(“warning”, “This is a warning message”)
log_message(“error”, “This is an error message”, “Error details”)
代码解释:

log_message 函数接受三个参数:level、message 和 extra_info。

level 参数指定日志级别,可以是 debug、info、warning 或 error。

message 参数是日志消息。

extra_info 参数是可选的附加信息。

根据 level 参数的不同,使用 logging 模块记录相应级别的日志
————————————————


1
 

给TA打赏
共{{data.count}}人
人已打赏
安全运维

安全运维之道:发现、解决问题的有效闭环

2024-4-14 20:59:36

安全运维

稳定性建设 – 架构优化的关键策略

2025-2-11 17:15:56

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索