面试前 30 分钟快速复习用
class MyClass:
def __new__(cls, *args): # 1. 创建实例(必须返回实例)
return super().__new__(cls)
def __init__(self, *args): # 2. 初始化实例
pass
def __del__(self): # 3. 销毁实例(不推荐依赖)
passdef __str__(self): # print() 时调用,面向用户
return "可读"
def __repr__(self): # repr() 时调用,面向开发者
return "ClassName(...)"def __eq__(self, other): return self.val == other.val # ==
def __lt__(self, other): return self.val < other.val # <
def __hash__(self): return hash(self.val) # set/dict 需要
# @total_ordering: 只需 __eq__ + __lt__,自动生成其余def __len__(self): # len(obj)
def __getitem__(self, k): # obj[k]
def __setitem__(self, k, v): # obj[k] = v
def __contains__(self, item): # item in obj
def __iter__(self): # for x in objdef __enter__(self): # with obj:
return self
def __exit__(self, *exc): # 离开 with 块
return False # True=吞异常def __get__(self, obj, objtype): # 访问属性
def __set__(self, obj, value): # 设置属性
def __delete__(self, obj): # 删除属性
# @property 是描述符的语法糖import functools
def my_decorator(func):
@functools.wraps(func) # 保留元信息!
def wrapper(*args, **kwargs):
# 前置逻辑
result = func(*args, **kwargs)
# 后置逻辑
return result
return wrapperdef repeat(n=2):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
for _ in range(n):
result = func(*args, **kwargs)
return result
return wrapper
return decorator
@repeat(n=3)
def say_hello(): print("Hello!")class CountCalls:
def __init__(self, func):
functools.update_wrapper(self, func)
self.func = func
self.count = 0
def __call__(self, *args, **kwargs):
self.count += 1
return self.func(*args, **kwargs)@A # 最后执行
@B # ↓
@C # 最先执行(靠近函数)
def func(): pass
# 等价于: A(B(C(func)))import asyncio
async def my_coroutine():
await asyncio.sleep(1)
return "result"
asyncio.run(my_coroutine())# gather: 并发执行,按顺序返回
results = await asyncio.gather(coro1(), coro2(), coro3())
# create_task: 动态创建任务
task = asyncio.create_task(coro())
result = await task
# as_completed: 按完成顺序返回
for coro in asyncio.as_completed(tasks):
result = await corotry:
result = await asyncio.wait_for(coro(), timeout=5.0)
except asyncio.TimeoutError:
print("超时!")sem = asyncio.Semaphore(10)
async with sem:
# 最多同时执行 10 个
await do_work()- Global Interpreter Lock,CPython 特性
- 同一时刻只有一个线程执行 Python 字节码
| 任务类型 | 多线程效果 | 推荐方案 |
|---|---|---|
| CPU 密集型 | ❌ 无效 | 多进程 |
| IO 密集型 | ✅ 有效 | 多线程/异步 |
# 1. 多进程
from multiprocessing import Pool
with Pool(4) as p:
results = p.map(func, data)
# 2. C 扩展(numpy 自动释放 GIL)
import numpy as np
result = np.sum(big_array)
# 3. 异步(单线程并发)
async def main():
await asyncio.gather(coro1(), coro2())type(object) # <class 'type'>
type(type) # <class 'type'>
isinstance(object, type) # True
isinstance(type, object) # TrueDog = type('Dog', (object,), {
'bark': lambda self: 'Woof!'
})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 MyClass(metaclass=SingletonMeta):
passclass PluginBase:
_plugins = {}
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
cls._plugins[cls.__name__] = clsimport sys
a = [1, 2, 3]
sys.getrefcount(a) # 2(a + 参数)
b = a
sys.getrefcount(a) # 3class Point:
__slots__ = ['x', 'y'] # 减少内存 30%-50%
def __init__(self, x, y):
self.x = x
self.y = yimport weakref
ref = weakref.ref(obj)
ref() # 访问对象(可能返回 None)import gc
gc.collect() # 手动触发
gc.disable() # 禁用自动 GC
gc.get_stats() # 查看统计from collections import Counter, defaultdict, deque, namedtuple
Counter(['a', 'b', 'a']) # {'a': 2, 'b': 1}
defaultdict(list) # 访问不存在的 key 自动创建 []
deque(maxlen=5) # 双端队列,自动丢弃旧元素
Point = namedtuple('Point', ['x', 'y'])from functools import lru_cache, partial, reduce
@lru_cache(maxsize=128) # 缓存装饰器
def fib(n): ...
square = partial(pow, exp=2) # 偏函数
reduce(lambda x, y: x+y, [1,2,3]) # 累积计算from itertools import chain, combinations, permutations
chain([1,2], [3,4]) # [1,2,3,4]
combinations('abc', 2) # ('a','b'), ('a','c'), ('b','c')
permutations('abc', 2) # 所有排列# 方式 1: __new__
class Singleton:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
# 方式 2: 装饰器
def singleton(cls):
instances = {}
def get_instance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return get_instanceclass ShapeFactory:
_registry = {'circle': Circle, 'rect': Rectangle}
@classmethod
def create(cls, shape_type, **kwargs):
return cls._registry[shape_type](**kwargs)class EventEmitter:
def __init__(self):
self._listeners = {}
def on(self, event, callback):
self._listeners.setdefault(event, []).append(callback)
def emit(self, event, *args):
for cb in self._listeners.get(event, []):
cb(*args)A: 两者都有。Python 代码先编译成字节码(.pyc),再由解释器执行。
A: 全局解释器锁,确保同一时刻只有一个线程执行字节码。CPU 密集型用多进程,IO 密集型用多线程/异步。
A: 浅拷贝只复制外层,深拷贝递归复制所有层。
A: *args 接收位置参数(元组),**kwargs 接收关键字参数(字典)。
A: 列表推导式 [] 立即生成所有元素,生成器表达式 () 惰性求值。
A: 运行时动态修改类或模块的行为。
最后更新: 2026-05-30