你可能不知道的实用 Python 功能

360影视 动漫周边 2025-05-18 05:33 3

摘要:from functools import partial# Instead of writing a new functiondef power_of_two(x): return pow(x, 2)# You can use partialpower_of

大多数开发人员都熟悉使用 with 语句进行文件操作:

with open('File.txt', 'r') as file: content = file.read# File is automatically closed after this block

但是, 内容管理器 可以做更多更多。它们是所有类型资源管理的完美选择:

from contextlib import contextmanagerimport time@contextmanagerdef timer: """Measure execution time of a code block.""" start = time.time try: yield # This is where the code within the 'with' block executes finally: end = time.time print(f"Elapsed time: {end - start:.4f} seconds")# Usagewith timer: # Some time-consuming operation result = sum(range(10_000_000))

contextlib 模块还提供了方便的工具,比如 suppress 用于抑制特定的异常 :

from contextlib import suppress# Instead of:try: os.remove('temp_file.txt')except FileNotFoundError: pass# You can write:with suppress(FileNotFoundError): os.remove('temp_file.txt')

functools.partial (?) 函数允许你创建带有预填充参数的新函数 :

from functools import partial# Instead of writing a new functiondef power_of_two(x): return pow(x, 2)# You can use partialpower_of_two = partial(pow, exp=2)# Create a base-2 logarithm functionimport mathlog2 = partial(math.log, base=2)print(log2(8)) # Outputs: 3.0

这对于回调函数或在处理 高阶函数 时特别有用。

解包运算符 * 和 ** 比我们大多数人都意识到的要更通用:

# Merging dictionaries (python 3.5+)defaults = {"colour": "red", "size": "medium"}user_settings = {"size": "large", "mode": "advanced"}settings = {**defaults, **user_settings}print(settings) # {'colour': 'red', 'size': 'large', 'mode': 'advanced'}# Extended unpacking (Python 3.0+)first, *middle, last = [1, 2, 3, 4, 5]print(middle) # [2, 3, 4]# Unpacking in function callsdef tag(name, **attributes): attr_str = ' '.join(f'{k}="{v}"' for k, v in attributes.items) return f''props = {"class": "button", "id": "submit-btn", "disabled": True}print(tag("button", **props)) #

省略号字面量不只是用于类型提示:

# As a placeholder for future codedef function_to_implement_later: ... # More explicit than 'pass'# In multidimensional numpy slicingimport numpy as nparray = np.random.rand(4, 4, 4)# Select the middle column from all rows in all matricesmiddle_column = array[:, 1, ...]

Python 函数是对象,可以有属性 :

def process_data(data, verbose=False): """Process the given data.""" if verbose or process_data.always_verbose: print("Processing data...") # Processing logic here return data# Add an attribute to the functionprocess_data.always_verbose = False# Later in your codeprocess_data.always_verbose = True # Enable verbose mode globally

这可以是在某些情况下作为全局变量的一个酷替代方案。

排序函数中的 key 参数比大多数人意识到的要强大得多:

# Sort strings by lengthwords = ["apple", "pear", "banana", "strawberry", "fig"]sorted_by_length = sorted(words, key=len)print(sorted_by_length) # ['fig', 'pear', 'apple', 'banana', 'strawberry']# Sort complex objectsfrom operator import attrgetter, itemgetter# For a list of dictionariesusers = [ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35}]sorted_users = sorted(users, key=itemgetter("age"))# For a list of objectsfrom collections import namedtuplePerson = namedtuple("Person", ["name", "age"])people = [Person("Alice", 30), Person("Bob", 25), Person("Charlie", 35)]sorted_people = sorted(people, key=attrgetter("age"))

collections 模块包含可以替代常见模式的数据结构 :

from collections import defaultdict, counter# Instead of:word_count = {}for word in text.split: if word not in word_count: word_count[word] = 0 word_count[word] += 1# You can use:word_count = defaultdict(int)for word in text.split: word_count[word] += 1# Or even simpler:word_count = Counter(text.split)print(word_count.most_common(5)) # Shows the 5 most common words

枚举模块有助于定义有意义的常量:

from enum import Enum, autoclass Status(Enum): PENDING = auto RUNNING = auto COMPLETED = auto FAILED = autodef process_job(job, status): if status == Status.RUNNING: print(f"Job {job} is still running") elif status == Status.COMPLETED: print(f"Job {job} completed successfully") # ...# So much more readable than numeric constantscurrent_status = Status.RUNNINGprocess_job("backup", current_status)

Python 3.7 引入了 数据类 (通过 PEP-557),它们减少了主要用于存储数据的类中的样板代码:

from dataclasses import dataclass, fieldfrom typing import List@dataclassclass Student: name: str student_id: int courses: List[str] = field(default_factory=list) active: bool = True def enroll(self, course): self.courses.append(course)# No need to write __init__, __repr__, __eq__, etc.student = Student("Jane Smith", 12345)student.enroll("Computer Science 101")print(student) # Student(name='Jane Smith', student_id=12345, courses=['Computer Science 101'], active=True)

对于具有固定属性集的类,__slots__ 可以显著减少内存使用:

class RegularPoint: def __init__(self, x, y): self.x = x self.y = yclass MemoryEfficientPoint: __slots__ = ['x', 'y'] def __init__(self, x, y): self.x = x self.y = y# The __slots__ version uses significantly less memory when many instances are createdimport sysregular = RegularPoint(3, 4)efficient = MemoryEfficientPoint(3, 4)print(sys.getsizeof(regular)) # Typically largerprint(sys.getsizeof(efficient)) # Typically smaller

在 Python 3.8 中,f 字符串增加了一个方便的调试功能: 已添加

x = 10y = 20print(f"{x=}, {y=}, {x+y=}")# Outputs: x=10, y=20, x+y=30from pathlib import Path# Create pathsdata_dir = Path("data")file_path = data_dir / "output.txt" # Path joining with / operator# Create directory if it doesn't existdata_dir.mkdir(exist_ok=True)# Write to a filefile_path.write_text("Hello, world!")# Read from a filecontent = file_path.read_text# Iterate over files in a directoryfor Python_file in data_dir.glob("*.py"): print(f"Found Python file: {python_file.name}")

来源:自由坦荡的湖泊AI一点号

相关推荐