Python __neg__ Method
Last modified November 18, 2026 Python 3.15
The unary minus operator calls __neg__ on its operand. A numeric
class implements it to return a new object representing the negated value,
leaving the original untouched. This article covers the basic wrapper,
vectors and matrices, a money class, temperature conversion, double negation
and the functional form operator.neg.
Basic unary negation
The hook takes no operand beyond self and returns a new value of
the same class. The example wraps a number and negates the stored value.
class Number:
def __init__(self, value):
self.value = value
def __neg__(self):
return Number(-self.value)
def __repr__(self):
return f"Number({self.value})"
num = Number(5)
neg_num = -num
print(neg_num)
print(num)
Applying - produced a new Number(-5). The second
line shows the original object is unchanged — mutation is not what unary minus
is for.
$ python basic_neg.py Number(-5) Number(5)
Negating a vector
For a vector, negation flips the sign of every component. This is the standard mathematical meaning of the operation.
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __neg__(self):
return Vector(-self.x, -self.y)
def __repr__(self):
return f"Vector({self.x}, {self.y})"
v = Vector(3, -4)
print(-v)
The first component becomes -3 and the second becomes 4. The original vector is preserved because the hook builds a fresh instance.
$ python vector_neg.py Vector(-3, 4)
Element-wise matrix negation
Nested data needs nested comprehensions: negate each element while keeping the row structure intact.
class Matrix:
def __init__(self, rows):
self.rows = [list(row) for row in rows]
def __neg__(self):
return Matrix([[-value for value in row] for row in self.rows])
def __repr__(self):
return f"Matrix({self.rows})"
m = Matrix([[1, 2], [3, -4]])
print(-m)
print(m)
Every element changed sign, including -4 which became
4. Printing the original afterwards confirms it was not
modified.
$ python matrix_neg.py Matrix([[-1, -2], [-3, 4]]) Matrix([[1, 2], [3, -4]])
Money and debt
In a financial class, negation can represent converting a balance into a debt. Attributes such as the currency must survive the operation.
class Money:
def __init__(self, amount, currency="USD"):
self.amount = amount
self.currency = currency
def __neg__(self):
return Money(-self.amount, self.currency)
def __add__(self, other):
if self.currency != other.currency:
raise ValueError("currencies must match")
return Money(self.amount + other.amount, self.currency)
def __repr__(self):
return f"{self.amount:.2f} {self.currency}"
balance = Money(100.50)
debt = -balance
print(debt)
print(balance + debt)
The negated balance keeps the currency, so adding it back to the original
is legal and yields zero. A currency check in __add__ keeps the
two operations consistent.
$ python money_neg.py -100.50 USD 0.00 USD
Negation and unit conversion
A temperature class can negate the numeric value while preserving the scale. Converting afterwards uses the usual formulas and produces a float for Fahrenheit.
class Temperature:
def __init__(self, value, scale="C"):
self.value = value
self.scale = scale
def __neg__(self):
return Temperature(-self.value, self.scale)
def convert(self):
if self.scale == "C":
return Temperature(self.value * 9 / 5 + 32, "F")
return Temperature((self.value - 32) * 5 / 9, "C")
def __repr__(self):
return f"{self.value}°{self.scale}"
temp = Temperature(25)
print(-temp)
print((-temp).convert())
Negation only changes the number; the scale stays Celsius. Converting the negated value gives -13.0 degrees Fahrenheit, matching the standard conversion formula.
$ python temperature_neg.py -25°C -13.0°F
Double negation
Applying unary minus twice should return to the original value. Because
__neg__ creates a new object each time, the final object is equal
in value but not identical to the starting one.
class Number:
def __init__(self, value):
self.value = value
def __neg__(self):
return Number(-self.value)
def __repr__(self):
return f"Number({self.value})"
n = Number(7)
twice = -(-n)
print(twice)
print(twice.value == n.value)
print(twice is n)
The value is restored and the equality check passes, but the identity check
is false. If identity preservation matters, the class could return
self for a zero value or cache results — but that is unusual for
numeric types.
$ python double_neg.py Number(7) True False
The operator.neg function
operator.neg(a) is the functional form of -a. It
calls the same hook and is useful when a callable is required.
import operator
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __neg__(self):
return Vector(-self.x, -self.y)
def __repr__(self):
return f"Vector({self.x}, {self.y})"
v = Vector(2, 5)
print(operator.neg(v))
print(operator.neg(7))
The custom vector and the built-in integer both dispatch through their own implementations. The functional form returns the same result as the operator syntax.
$ python operator_neg.py Vector(-2, -5) -7
Best practices
A few rules keep - predictable:
- Return a new object; do not mutate
self. - Preserve attributes such as units or currency that negation should not change.
- Return the same type as the operand whenever that makes sense.
- Implement
__pos__alongside__neg__for a complete unary API. - Use
operator.negwhen you need a callable form of the operator.
Source
object.__neg__ - Python documentation
Emulating numeric types - Python documentation
operator.neg - Python documentation
In this article we have implemented the __neg__ method. We have
covered the unary minus dispatch, vectors and matrices, money, temperature
conversion, double negation and operator.neg. For related topics,
see Python __pos__ method,
Python __abs__ method, and
Python operators.
Author
List all Python tutorials.