使用 IronPython 重载魔法方法
我试图在 C# 类中定义幂运算符 **(我们将其称为 Foo 类)。我已经重写了 __pow__(),它为我提供了 Foo ** int
类型操作所需的行为。不幸的是,我还需要定义 int ** Foo ,并且使用动态值和重载 __pow__() 都无法给我带来所需的行为;我总是收到错误** 不支持的操作数类型:'int' 和 'Foo'
。我已使用运算符关键字成功覆盖了 C# 识别的运算符; Foo / int
和 int / Foo
均成功运行。有没有办法用 __pow__() 来做到这一点?先感谢您。
I'm attempting to define the power operator ** in a C# class (we'll call it class Foo). I've overridden __pow__()
, which gets me the desired behavior for operations of type Foo ** int
. Unfortunately, I need to also define int ** Foo
, and neither using dynamic values nor overloading __pow__()
gives me the desired behavior; I always receive the error unsupported operand type(s) for **: 'int' and 'Foo'
. I have successfully overridden C#-recognized operators using the operator keyword; both Foo / int
and int / Foo
are functioning successfully. Is there any way to do this with __pow__()
? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论