一、__call__ 方法概述
1. 什么是 __call__?
__call__ 方法允许类的实例像函数一样被调用。实现了 __call__ 方法的对象称为可调用对象。
2. 基本语法
class CallableClass: def __call__(self, *args, **kwargs): # 当实例像函数一样调用时执行 return "对象被调用了"# 使用obj = CallableClass()result = obj() # 调用 __call__ 方法print(result) # 输出: 对象被调用了
3. 为什么需要 __call__?
状态保持:在多次调用之间保持状态
函数式编程:创建函数对象
装饰器实现:类装饰器的基础
API设计:创建更直观的接口
二、基础用法
1. 最简单的可调用对象
class Greeter: """简单的可调用对象""" def __init__(self, greeting="Hello"): self.greeting = greeting def __call__(self, name): return f"{self.greeting}, {name}!"# 使用greet = Greeter("你好")print(greet("张三")) # 你好, 张三!greet2 = Greeter("Good morning")print(greet2("Alice")) # Good morning, Alice!
2. 带参数的可调用对象
class Calculator: """计算器类,可像函数一样调用""" def __call__(self, operation, a, b): """根据操作符执行计算""" operations = { '+': a + b, '-': a - b, '*': a * b, '/': a / b if b != 0 else float('inf'), '**': a ** b } return operations.get(operation, "不支持的操作")# 使用calc = Calculator()print(calc('+', 10, 5)) # 15print(calc('-', 10, 5)) # 5print(calc('*', 10, 5)) # 50print(calc('/', 10, 5)) # 2.0
三、状态保持和计数器
1. 函数调用计数器
class Counter: """函数调用计数器""" def __init__(self): self.count = 0 self.total = 0 def __call__(self, *args, **kwargs): self.count += 1 self.total += 1 print(f"函数被调用第 {self.count} 次") return self.count def reset(self): """重置计数器""" self.count = 0 print("计数器已重置") def stats(self): """返回统计信息""" return { 'current_count': self.count, 'total_calls': self.total }# 使用counter = Counter()print(counter()) # 函数被调用第 1 次print(counter()) # 函数被调用第 2 次print(counter()) # 函数被调用第 3 次counter.reset()print(counter()) # 函数被调用第 1 次print(counter.stats()) # {'current_count': 1, 'total_calls': 4}
2. 带缓存的函数
class CachedFunction: """带缓存的函数""" def __init__(self, func): self.func = func self.cache = {} self.hits = 0 self.misses = 0 def __call__(self, *args): if args in self.cache: self.hits += 1 print(f"缓存命中: {args} -> {self.cache[args]}") return self.cache[args] self.misses += 1 result = self.func(*args) self.cache[args] = result print(f"计算新值: {args} -> {result}") return result def stats(self): """返回缓存统计""" return { 'hits': self.hits, 'misses': self.misses, 'hit_rate': self.hits / (self.hits + self.misses) * 100 if (self.hits + self.misses) > 0 else 0, 'cache_size': len(self.cache) } def clear(self): """清空缓存""" self.cache.clear() self.hits = 0 self.misses = 0 print("缓存已清空")# 使用@CachedFunction # 作为装饰器使用def fibonacci(n): """计算斐波那契数列""" if n < 2: return n return fibonacci(n-1) + fibonacci(n-2)print("计算斐波那契数列:")print(f"fib(5) = {fibonacci(5)}")print(f"fib(5) = {fibonacci(5)}") # 第二次调用,使用缓存print(f"fib(6) = {fibonacci(6)}")print("\n缓存统计:")print(fibonacci.stats())
3. 函数执行时间测量
import timeclass Timer: """函数执行时间测量器""" def __init__(self, name=None): self.name = name self.total_time = 0 self.call_count = 0 def __call__(self, func): """作为装饰器使用""" def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) elapsed = time.time() - start self.total_time += elapsed self.call_count += 1 func_name = self.name or func.__name__ print(f"{func_name} 执行时间: {elapsed:.4f}秒") return result return wrapper def stats(self): """返回统计信息""" return { 'function': self.name, 'calls': self.call_count, 'total_time': self.total_time, 'avg_time': self.total_time / self.call_count if self.call_count > 0 else 0 }# 使用timer = Timer("排序函数")@timerdef slow_function(): """模拟慢速函数""" time.sleep(0.5) return "完成"@timerdef quick_function(): """快速函数""" time.sleep(0.1) return "快速完成"print("第一次调用:")slow_function()quick_function()print("\n第二次调用:")slow_function()quick_function()print("\n统计信息:")print(timer.stats())
四、函数工厂和闭包
1. 幂函数工厂
class PowerFactory: """幂函数工厂""" def __init__(self): self.created_functions = [] def __call__(self, exponent): """创建指定指数的幂函数""" def power_func(x): return x ** exponent # 记录创建的函数 self.created_functions.append({ 'exponent': exponent, 'function': power_func }) return power_func def list_functions(self): """列出所有创建的幂函数""" for i, func_info in enumerate(self.created_functions): print(f"{i+1}. x^{func_info['exponent']}")# 使用factory = PowerFactory()square = factory(2) # 创建平方函数cube = factory(3) # 创建立方函数fourth = factory(4) # 创建四次方函数print(f"5的平方: {square(5)}")print(f"5的立方: {cube(5)}")print(f"5的四次方: {fourth(5)}")print("\n已创建的幂函数:")factory.list_functions()
2. 累加器函数
class Accumulator: """累加器函数""" def __init__(self, initial=0): self.total = initial self.history = [initial] def __call__(self, value=None): """累加数值""" if value is None: return self.total self.total += value self.history.append(value) return self.total def reset(self, value=0): """重置累加器""" self.total = value self.history = [value] print(f"累加器重置为 {value}") def average(self): """返回平均值""" if len(self.history) <= 1: return 0 return (self.total - self.history[0]) / (len(self.history) - 1)# 使用acc = Accumulator(10)print(acc(5)) # 15print(acc(3)) # 18print(acc(7)) # 25print(f"当前总和: {acc()}") # 25print(f"平均值: {acc.average():.2f}")acc.reset(0)print(acc(1)) # 1print(acc(2)) # 3
五、总结
1. 设计原则
保持简单:__call__ 应该做一件事,做好它
文档清晰:说明调用时的行为和参数
类型提示:使用类型注解提高可读性
一致性:多次调用行为应该一致
性能考虑:注意调用频率和缓存策略
2. 常见陷阱
# 陷阱1:忘记返回 self(链式调用)class BadChain: def __call__(self, x): self.value = x # 应该返回 self 支持链式调用# 陷阱2:修改全局状态class BadCounter: count = 0 # 类变量,所有实例共享 def __call__(self): BadCounter.count += 1 return BadCounter.count# 陷阱3:忽略性能class SlowCallable: def __call__(self, *args): # 每次都执行耗时操作,应该考虑缓存 return sum(args) / len(args)# 陷阱4:异常处理不当class BadValidator: def __call__(self, value): if not value: return False # 应该抛出异常或明确返回
3. 最佳实践
class BestPracticeCallable: """最佳实践示例""" def __init__(self, initial_value=0): """初始化状态""" self._value = initial_value self._call_count = 0 self._history = [] def __call__(self, increment=1): """ 调用时增加计数 Args: increment: 增加的量,默认为1 Returns: 当前值 """ self._call_count += 1 self._value += increment self._history.append((self._call_count, self._value)) return self._value def reset(self, value=0): """重置状态""" self._value = value self._call_count = 0 self._history.clear() def stats(self): """返回统计信息""" return { 'current_value': self._value, 'total_calls': self._call_count, 'history': self._history.copy() } @property def value(self): """当前值属性""" return self._value# 使用counter = BestPracticeCallable(10)print(counter()) # 11print(counter(5)) # 16print(counter(2)) # 18print(f"统计: {counter.stats()}")counter.reset()print(f"重置后: {counter()}")
__call__ 方法是Python中非常强大的特性,它让对象可以像函数一样被调用,同时保持对象的状态和功能。