Using complex, real and imag attributes are computed collectly as
follows:
>>> 1+2j
(1+2j)
>>> z = 1+2j
>>> z.real
1.0
>>> z.imag
2.0
>>> (1+2j).real
1.0
>>> (1+2j).imag
2.0
But, if there's no explicit literal boundary, it seems to break
consistensy in operator precedence:
>>> 1+2j.real # addition succeeds j-suffux
1.0
>>> 1+2j.imag # addition precedes (j-suffix and) attribute
reference
3.0
>>> 0+1+2j.real # same as above
1.0
>>> 0+1+2j.imag
3.0
>>> 1+0+2j.imag
3.0
>>> 1+0+2j.real
1.0
>>> 1+(2j).imag # brace puts no bless
3.0
>>> 1*1+2j.imag # star fails to steer
3.0
This happens at least on Python 2.3.5 (OSX bundled), Python 2.4.2
(build from ports, FreeBSD 5.4).
# Practically, of course, you always explicit (1+2j) to construct complex
thus hardly troubled with this :) but it would be happy for beginners to
mention it on standard tutorial or something.
|