使用 IronPython 重载魔法方法

发布于 2024-09-26 05:36:26 字数 314 浏览 4 评论 0原文

我试图在 C# 类中定义幂运算符 **(我们将其称为 Foo 类)。我已经重写了 __pow__(),它为我提供了 Foo ** int 类型操作所需的行为。不幸的是,我还需要定义 int ** Foo ,并且使用动态值和重载 __pow__() 都无法给我带来所需的行为;我总是收到错误** 不支持的操作数类型:'int' 和 'Foo'。我已使用运算符关键字成功覆盖了 C# 识别的运算符; Foo / intint / 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文