2

Is it possible to overload ++ operators in Python?

2
  • Is this a duplicate of stackoverflow.com/questions/728361/… ..? Commented Apr 21, 2009 at 22:14
  • 1
    ..err, then why do you want to overload an operator that doesn't exist..? Commented Apr 22, 2009 at 15:24

5 Answers 5

20

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

Sign up to request clarification or add additional context in comments.

Comments

18

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.

Comments

7

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.

Comments

5

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)

2 Comments

Edward Z. Yang: That's because it's trying to call a function. 3() isn't a function.
The original point was that if it were a right-binding unary operator, 3 ++ 4 should not be valid, because it's two tokens smushed together like 3 5. However, this is a bit misguided since Python will treat the plus token differently depending on context. I will, however, reiterate: ++ is definitely not an operator: see the grammar factor: ('+'|'-'|'~') factor | power and also how factor is handled in compiler/transformer.py. See also Jake's hack.
5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.