由于闭包和 Python 在对象自省上的灵活性,我们可以提炼出 operator 函数的创建 。
# 一个创建闭包的函数,实现了二元运算的逻辑_MISSING = object()def _create_binary_op(name: str, operator: str) -> Any: """Create a binary operation function. The `name` parameter specifies the name of the special method used for the binary operation (e.g. `sub` for `__sub__`). The `operator` name is the token representing the binary operation (e.g. `-` for subtraction). """ lhs_method_name = f"__{name}__" def binary_op(lhs: Any, rhs: Any, /) -> Any: """A closure implementing a binary operation in Python.""" rhs_method_name = f"__r{name}__" # lhs.__*__ lhs_type = type(lhs) try: lhs_method = debuiltins._mro_getattr(lhs_type, lhs_method_name) except AttributeError: lhs_method = _MISSING # lhs.__r*__ (for knowing if rhs.__r*__ should be called first) try: lhs_rmethod = debuiltins._mro_getattr(lhs_type, rhs_method_name) except AttributeError: lhs_rmethod = _MISSING # rhs.__r*__ rhs_type = type(rhs) try: rhs_method = debuiltins._mro_getattr(rhs_type, rhs_method_name) except AttributeError: rhs_method = _MISSING call_lhs = lhs, lhs_method, rhs call_rhs = rhs, rhs_method, lhs if ( rhs_type is not _MISSING # Do we care? and rhs_type is not lhs_type # Could RHS be a subclass? and issubclass(rhs_type, lhs_type) # RHS is a subclass! and lhs_rmethod is not rhs_method # Is __r*__ actually different? ): calls = call_rhs, call_lhs elif lhs_type is not rhs_type: calls = call_lhs, call_rhs else: calls = (call_lhs,) for first_obj, meth, second_obj in calls: if meth is _MISSING: continue value = meth(first_obj, second_obj) if value is not NotImplemented: return value else: exc = TypeError( f"unsupported operand type(s) for {operator}: {lhs_type!r} and {rhs_type!r}" ) exc._binary_op = operator raise exc
推荐阅读
- 万字干货,Python语法大合集,一篇文章带你入门
- 冷泡茶的做法,乌龙茶冷泡茶的好处有哪些
- EditPlus——一款小巧功能强大的老牌代码文本编辑器
- HttpClient三个超时时间详解
- 历史上著名的连环杀手 古代有名的杀手
- 辽国第五位皇帝辽景宗 辽世宗与辽穆宗的关系
- 形容外行人的歇后语 外行的歇后语是什么
- 沉鱼落雁的成语故事 沉鱼落雁是成语吗
- 处暑的气候特点 节气与气候及农业生产的关系
- 李元吉没有当皇帝的可能嘛 李建成和李元吉为什么要排挤李世民