Issue1543347
Created on 2006-08-20 05:59 by ymasuda, last changed 2006-08-20 16:52 by tim_one.
| 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) | 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_one) | 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 |
| 2006-08-20 05:59:55 | ymasuda | create | |