This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Operator precedence inconsistent for complex literal
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, tim.peters, ymasuda
Priority: normal Keywords:

Created on 2006-08-20 05:59 by ymasuda, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg29574 - (view) Author: [N/A] (ymasuda) Date: 2006-08-20 05:59
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.
msg29575 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-08-20 14:37
Logged In: YES 
user_id=849994

I can't see anything inconsistent here. Attribute access
always happens before "+" or "*" are applied.
msg29576 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2006-08-20 16:52
Logged In: YES 
user_id=31435

Note that Python doesn't have complex literals, only
imaginary literals:  1+2j is the integer 1 added to the
imaginary literal 2j.  IOW, it's the same as (1)+(2j) = 1 +
complex(0, 2).  Everything follows from that; e.g.,
0+1+2j.imag is parsed as (0+1)+(2j.imag) = 1 + 2.0 = 3.0.
History
Date User Action Args
2022-04-11 14:56:19adminsetgithub: 43862
2006-08-20 05:59:55ymasudacreate