[ python-Bugs-1543347 ] Operator precedence inconsistent for complex literal

SourceForge.net noreply at sourceforge.net
Sun Aug 20 18:52:21 CEST 2006


Bugs item #1543347, was opened at 2006-08-20 01:59
Message generated for change (Comment added) made by tim_one
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1543347&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.3
Status: Closed
Resolution: Works For Me
Priority: 5
Submitted By: [N/A] (ymasuda)
Assigned to: Nobody/Anonymous (nobody)
Summary: Operator precedence inconsistent for complex literal

Initial Comment:
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.

----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2006-08-20 12:52

Message:
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.

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2006-08-20 10:37

Message:
Logged In: YES 
user_id=849994

I can't see anything inconsistent here. Attribute access
always happens before "+" or "*" are applied.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1543347&group_id=5470


More information about the Python-bugs-list mailing list