An identity function on int. It exists because int is registered as a Complex number, and every Complex must be able to answer conjugate().
Int methodPython 2.6+Live demo
Common call
n.conjugate()
Returns
n — always, for every int
Replaces
isinstance(v, complex) branching in numeric code
Watch out
on int it does nothing; the meaning only appears on complex
int.conjugate()
→ int
Demo
Live evaluation
Try:
Inputs
nintinteger
Output
(5).conjugate()
5
Every case returns the input unchanged, and that is the correct answer rather than a missing implementation. The complex conjugate flips the sign of the imaginary part: (3+4j).conjugate() is (3-4j). An int has no imaginary part, so there is nothing to flip and the value comes back as it went in. The method exists at all because Python registers int in the numeric tower as a Complex number, and that contract requires conjugate().
Common patterns
Magnitude squared for any number type
z * z.conjugate() is real for complex and just a square for int and float.
magnitude_sq = (z * z.conjugate()).real
Type-agnostic inner product
The conjugate belongs in the formula; on real inputs it costs nothing.
dot = sum(a * b.conjugate() fora, binzip(xs, ys))
Write the maths once
The same expression stays correct whether the caller passes ints or complex.
defnorm(z):
return (z * z.conjugate()).real ** 0.5
Examples
1. Returns itself
(5).conjugate()
Returns
5
2. Zero
(0).conjugate()
Returns
0
3. Sign is kept
(-7).conjugate()
Returns
-7
4. Large values
(123456).conjugate()
Returns
123456
5. Where it matters
(3+4j).conjugate()
Returns
(3-4j)
6. float too
(2.5).conjugate()
Returns
2.5
Pitfalls
1. It is not negation
Conjugation flips the sign of the IMAGINARY part only. Reaching for it to negate a real number gives the value straight back, silently, with no error to point at the mistake.
Expected -5
(5).conjugate()
5
Negate instead
-5
-5
2. On int alone it looks like dead code
A call that always returns its input reads like a bug to anyone skimming. Worth a comment when it appears in numeric code, because deleting it quietly breaks the complex case the formula was written for.
Tempting to delete
dot = sum(a * bfora, binzip(xs, ys))
wrong once ys holds complex numbers
Keep it, note why
dot = sum(a * b.conjugate() fora, binzip(xs, ys)) # correct for complex too
correct for every numeric type
3. bool inherits it and returns an int
True is an int, so it has conjugate — but the result comes back as 1, not True. A surprise if the value flows into something that formats or compares booleans.
Loses the bool
True.conjugate()
1
Keep the type
bool(True.conjugate())
True
When to use
Use it
Numeric code that must accept int, float and complex through one path
Inner products, norms and magnitude formulas written in their general form
Implementing numbers.Complex on a custom numeric type
Reach for something else
You know the value is an int — it returns the input unchanged
You meant to negate → use -n
You want the real or imaginary part → the .real and .imag attributes
Notes
Complexity
O(1) — returns the same object
Return
The int itself; CPython returns the same object for small ints
CPython impl
Objects/longobject.c :: long_long (registered as int.conjugate)
Because PEP 3141 registers int in the numeric tower as a numbers.Complex, and that interface requires conjugate(), real and imag. int satisfies it in the only way a real number can: the conjugate is itself, imag is 0, and real is the value.