Python3 高级编程话题详解

Python3 高级编程话题详解

Python 作为一门多范式编程语言,在高级编程领域有许多值得深入探讨的话题。以下是一些关键的高级编程概念和技术:

1. 元编程(Metaprogramming)

元编程是指编写能操作其他程序(或自身)作为数据的程序。

# 动态创建类

class DynamicClassCreator:

@classmethod

def create(cls, name, bases, namespace):

return type(name, bases, namespace)

MyClass = DynamicClassCreator.create('MyClass', (), {'x': 42})

# 使用类装饰器

def add_method(cls):

def method(self):

return "Added method"

cls.new_method = method

return cls

@add_method

class MyClass:

pass

2. 描述符协议(Descriptor Protocol)

描述符是实现了 get, set 或 delete 方法的对象。

class ValidatedAttribute:

def __init__(self, name, type_):

self.name = name

self.type_ = type_

def __get__(self, instance, owner):

return instance.__dict__[self.name]

def __set__(self, instance, value):

if not isinstance(value, self.type_):

raise TypeError(f"Expected {self.type_}")

instance.__dict__[self.name] = value

class Person:

name = ValidatedAttribute("name", str)

age = ValidatedAttribute("age", int)

3. 上下文管理器(Context Managers)

不仅可以使用 with 语句,还可以自定义上下文管理器。

from contextlib import contextmanager

@contextmanager

def managed_resource(*args, **kwargs):

resource = acquire_resource(*args, **kwargs)

try:

yield resource

finally:

release_resource(resource)

# 使用示例

with managed_resource(timeout=10) as r:

r.do_something()

4. 协程和异步编程(Coroutines & Async/Await)

import asyncio

async def fetch_data(url):

print(f"Fetching {url}")

await asyncio.sleep(2) # 模拟IO操作

return f"Data from {url}"

async def main():

task1 = asyncio.create_task(fetch_data("url1"))

task2 = asyncio.create_task(fetch_data("url2"))

results = await asyncio.gather(task1, task2)

print(results)

asyncio.run(main())

5. 多线程与多进程

# 多线程

from threading import Thread

import concurrent.futures

def task(n):

return n * n

with concurrent.futures.ThreadPoolExecutor() as executor:

results = executor.map(task, range(10))

# 多进程

from multiprocessing import Pool

def cpu_intensive(n):

return n * n

if __name__ == '__main__':

with Pool() as pool:

results = pool.map(cpu_intensive, range(10))

6. 类型注解和静态类型检查

from typing import List, Dict, Tuple, Optional, Callable, TypeVar, Generic

T = TypeVar('T')

class Stack(Generic[T]):

def __init__(self) -> None:

self.items: List[T] = []

def push(self, item: T) -> None:

self.items.append(item)

def pop(self) -> T:

return self.items.pop()

def greet(name: str) -> str:

return f"Hello, {name}"

7. 数据类(Data Classes)

from dataclasses import dataclass, field

from typing import List

@dataclass(order=True)

class Person:

name: str

age: int

hobbies: List[str] = field(default_factory=list)

# 自动生成 __init__, __repr__, __eq__ 等方法

p1 = Person("Alice", 30)

p2 = Person("Bob", 25, ["reading", "hiking"])

8. 函数式编程特性

from functools import partial, reduce

from operator import add

# 偏函数

pow_two = partial(pow, exp=2)

print(pow_two(5)) # 25

# 函数组合

def compose(*funcs):

return reduce(lambda f, g: lambda x: f(g(x)), funcs)

add_then_double = compose(lambda x: x*2, lambda y: y+10)

print(add_then_double(5)) # 30

# 不可变数据结构

from collections import namedtuple

Point = namedtuple('Point', ['x', 'y'])

p = Point(1, 2)

9. 元类(Metaclasses)

class SingletonMeta(type):

_instances = {}

def __call__(cls, *args, **kwargs):

if cls not in cls._instances:

cls._instances[cls] = super().__call__(*args, **kwargs)

return cls._instances[cls]

class SingletonClass(metaclass=SingletonMeta):

pass

a = SingletonClass()

b = SingletonClass()

print(a is b) # True

10. 性能优化技术

# 使用 __slots__ 减少内存使用

class Optimized:

__slots__ = ['x', 'y']

def __init__(self, x, y):

self.x = x

self.y = y

# 使用生成器处理大数据集

def large_dataset():

for i in range(1000000):

yield i * 2

# 使用缓存

from functools import lru_cache

@lru_cache(maxsize=128)

def expensive_function(x):

return x ** x

11. 抽象基类(ABC)

from abc import ABC, abstractmethod

class Shape(ABC):

@abstractmethod

def area(self):

pass

class Circle(Shape):

def __init__(self, radius):

self.radius = radius

def area(self):

return 3.14 * self.radius ** 2

12. 协议和结构化类型(Duck Typing)

from typing import Protocol, runtime_checkable

@runtime_checkable

class SupportsClose(Protocol):

def close(self) -> None: ...

def close_all(items: list[SupportsClose]) -> None:

for item in items:

item.close()

13. 装饰器高级用法

# 带参数的装饰器

def repeat(num_times):

def decorator(func):

def wrapper(*args, **kwargs):

for _ in range(num_times):

result = func(*args, **kwargs)

return result

return wrapper

return decorator

@repeat(num_times=3)

def greet(name):

print(f"Hello {name}")

# 类装饰器

class CountCalls:

def __init__(self, func):

self.func = func

self.num_calls = 0

def __call__(self, *args, **kwargs):

self.num_calls += 1

print(f"Call {self.num_calls}")

return self.func(*args, **kwargs)

@CountCalls

def say_hello():

print("Hello")

14. 动态导入和插件架构

import importlib

# 动态导入模块

module = importlib.import_module('math')

sqrt_func = getattr(module, 'sqrt')

# 插件架构

from importlib import resources

import plugins

def load_plugins():

for entry in resources.contents(plugins):

if entry.endswith('.py') and not entry.startswith('_'):

module_name = f"plugins.{entry[:-3]}"

plugin = importlib.import_module(module_name)

if hasattr(plugin, 'register'):

plugin.register()

15. 并发模式

# 使用队列的生产者-消费者模式

import queue

import threading

def producer(q):

for i in range(5):

q.put(i)

print(f"Produced {i}")

def consumer(q):

while True:

item = q.get()

if item is None: # 哨兵值

break

print(f"Consumed {item}")

q.task_done()

q = queue.Queue()

threads = [

threading.Thread(target=producer, args=(q,)),

threading.Thread(target=consumer, args=(q,))

]

for t in threads:

t.start()

q.join()

q.put(None) # 结束信号

16. 性能分析

import cProfile

import pstats

from io import StringIO

def profile_func(func, *args, **kwargs):

pr = cProfile.Profile()

pr.enable()

result = func(*args, **kwargs)

pr.disable()

s = StringIO()

ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')

ps.print_stats()

print(s.getvalue())

return result

def slow_function():

total = 0

for i in range(100000):

total += i

return total

profile_func(slow_function)

这些高级话题展示了 Python 的强大功能和灵活性。掌握这些概念可以帮助你编写更高效、更可维护和更 Pythonic 的代码。

[an error occurred while processing the directive]