您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 是否可以使用__rmod__覆盖str的%行为?

5b51 2022/1/14 8:23:10 python 字数 3569 阅读 628 来源 www.jb51.cc/python

我想这样做: x %doSomething% y 对于任何x和任何y都很容易做到(见下面的代码),除非x是str. 是否有任何方法(例如添加特殊方法或引发特定错误)导致旧样式字符串格式化失败(类似于1%doSomthing如何通过TypeError失败)并恢复到doSomething对象中定义的__rmod__方法? class BinaryMessage(object): def __ini

概述

x %doSomething% y

对于任何x和任何y都很容易做到(见下面的代码),除非x是str.

是否有任何方法(例如添加特殊方法或引发特定错误)导致旧样式字符串格式化失败(类似于1%doSomthing如何通过TypeError失败)并恢复到doSomething对象中定义的__rmod__方法

class BinaryMessage(object):
  def __init__(self,fn):
    self._fn = fn
  def __rmod__(self,LHS):
    return BinaryMessagePartial(self._fn,LHS)

class BinaryMessagePartial(object):
  def __init__(self,fn,LHS):
    self._fn = fn
    self._LHS = LHS
  def __mod__(self,RHS):
    return self._fn(self._LHS,RHS)

def _doSomething(a,b):
  return a + b

doSomething = BinaryMessage(_doSomething)

result = 5 %doSomething% 6
assert result == 11

不幸的是,这在Python中目前是不可能的.行为在评估循环中是硬编码的:

TARGET(BINARY_MODULO) {
    PyObject *divisor = POP();
    PyObject *dividend = TOP();
    PyObject *res = PyUnicode_CheckExact(dividend) ?
        PyUnicode_Format(dividend,divisor) :
        PyNumber_Remainder(dividend,divisor);

(从Python 3.5 source code开始,PyUnicode是Python str类型).

这是不幸的,因为对于其他所有类型,您可以通过使用右侧操作数的子类来阻止LHS .__ mod__方法;从documentation

Note: If the right operand’s type is a subclass of the left operand’s type and that subclass provides the reflected method for the operation,this method will be called before the left operand’s non-reflected method. This behavior allows subclasses to override their ancestors’ operations.

这将是唯一的选项,str%other永远不会返回NotImplemented,所有RHS类型都被接受(实际的str.__mod__ method只接受RHS的str对象,但在这种情况下不调用).

我认为这是Python中的一个错误,提交为issue #28598.

总结

以上是编程之家为你收集整理的python – 是否可以使用__rmod__覆盖str的%行为?全部内容,希望文章能够帮你解决python – 是否可以使用__rmod__覆盖str的%行为?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶