是否可以超载++ Python 中的运算符?

发布于 2024-07-18 07:16:14 字数 29 浏览 4 评论 0原文

Python 中是否可以重载 ++ 运算符?

Is it possible to overload ++ operators in Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

妳是的陽光 2024-07-25 07:16:14

Python 中没有 ++ 运算符(也没有“--”)。 递增通常使用 += 运算符来完成。

There is no ++ operator in Python (nor '--'). Incrementing is usually done with the += operator instead.

友欢 2024-07-25 07:16:14

不,不可能重载一元 ++ 运算符,因为它在 Python 中根本不是运算符。

只有 Python 语法允许的运算符(子集)(那些已在该语言中具有一个或多个用途的运算符)可能会被重载。

这些是有效的Python运算符,并且此页面列出了您可以定义来重载它们的方法(带有两个前导和尾随下划线的方法)。

与其他语言中常用的 i++ 不同,Python 中的写法是 i += 1。

在 Python 中,+ 符号的右侧需要一个操作数。 它的左侧可能还有一个操作数,在这种情况下,它将被解释为二进制而不是一元运算符。 +5, ++5, ..., ++++++5 都是有效的 Python 表达式(全部计算结果为 5),7 + 5, 7 ++ 5, ..., 7 ++++ 也是如此++++ 5(全部计算结果为 7 + (+...+5) = 12)。 5+ 不是有效的 Python。 另请参阅此问题

替代想法:根据您实际想要使用 ++ 运算符的目的,您可能需要考虑重载 一元(前缀)加运算符。 请注意,这可能会导致一些看起来很奇怪的代码。 其他查看您的代码的人可能会认为这是一个空操作并感到困惑。

Nope, it is not possible to overload the unary ++ operator, because it is not an operator at all in Python.

Only (a subset of) the operators that are allowed by the Python syntax (those operators that already have one or more uses in the language) may be overloaded.

These are valid Python operators, and this page lists the methods that you can define to overload them (the ones with two leading and trailing underscores).

Instead of i++ as commonly used in other languages, in Python one writes i += 1.

In python the + sign needs an operand to its right. It may also have an operand to its left, in which case it will be interpreted as a binary instead of a unary operator. +5, ++5, ..., ++++++5 are all valid Python expressions (all evaluating to 5), as are 7 + 5, 7 ++ 5, ..., 7 ++++++++ 5 (all evaluating to 7 + (+...+5) = 12). 5+ is not valid Python. See also this question.

Alternative idea: Depending on what you actually wanted to use the ++ operator for, you may want to consider overloading the unary (prefix) plus operator. Note, thought, that this may lead to some odd looking code. Other people looking at your code would probably assume it's a no-op and be confused.

浸婚纱 2024-07-25 07:16:14

每个人都说得很好,我只是想澄清另一件事。 打开 Python 解释器并检查一下:

>>> i = 1
>>> ++i
1
>>> i
1

Python 中没有 ++(或 --)运算符。 它的行为(而不是语法错误)的原因是 + 和 - 是有效的一元运算符,其作用基本上就像数字上的符号一样。 您可以将 ++i 视为“+(+i)”,将 --i 视为“-(-i)”。 期望 ++i 能够像任何其他语言一样工作会导致绝对阴险的错误搜寻。 C 程序员:请注意。

直接的 i++i-- 确实会失败,无论其价值如何。

Everyone makes good points, I'd just like to clear up one other thing. Open up a Python interpreter and check this out:

>>> i = 1
>>> ++i
1
>>> i
1

There is no ++ (or --) operator in Python. The reason it behaves as it did (instead of a syntax error) is that + and - are valid unary operators, acting basically like a sign would on digits. You can think of ++i as a "+(+i)", and --i as "-(-i)". Expecting ++i to work like in any other language leads to absolutely insidious bug-hunts. C programmers: ye be warned.

A straight i++ or i-- does fail adequately, for what it's worth.

失去的东西太少 2024-07-25 07:16:14

嗯,Python 中不存在 ++ 运算符,因此您确实无法重载它。

当你做类似的事情时会发生什么:

1 ++ 2

实际上是

1 + (+2)

Well, the ++ operator doesn't exist in Python, so you really can't overload it.

What happens when you do something like:

1 ++ 2

is actually

1 + (+2)
又怨 2024-07-25 07:16:14

你可以破解它,尽管这会带来一些不良后果:

class myint_plus:
    def __init__(self,myint_instance):
        self.myint_instance = myint_instance

    def __pos__(self):
        self.myint_instance.i += 1
        return self.myint_instance

class myint:
    def __init__(self,i):
        self.i = i

    def __pos__(self):
        return myint_plus(self)

    def __repr__(self):
        return self.i.__repr__()


x = myint(1)
print x
++x
print x

输出是:

1
2

You could hack it, though this introduces some undesirable consequences:

class myint_plus:
    def __init__(self,myint_instance):
        self.myint_instance = myint_instance

    def __pos__(self):
        self.myint_instance.i += 1
        return self.myint_instance

class myint:
    def __init__(self,i):
        self.i = i

    def __pos__(self):
        return myint_plus(self)

    def __repr__(self):
        return self.i.__repr__()


x = myint(1)
print x
++x
print x

the output is:

1
2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文