Complex evaluation bug

Christophe chris.cavalaria at free.fr
Fri May 19 03:54:30 EDT 2006


Gary Herron a écrit :
> of wrote:
> 
>> a = 1+3j
>> complex(str(a))
>>
>> Why does this not work ? It should
>>  
>>
> Says who?
> By normal conventions in Python, "str" attempts only to make a "nice" 
> human readable representation.  The function "repr" is usually expected 
> to provide output that can be parsed back into the original object.  
> (Although for the  numeric complex type the two produce identical results.)
> 
> Further, constructors are rarely expected to parse a string 
> representation to return an object.  The function "eval" is usually 
> expected to provide that functionality.
> 
> So, putting them together, you could expect
>    eval(repr(a))
> to reproduce a, and in fact it does so.

Says who ?

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> repr(1+3j)
'(1+3j)'
 >>> complex(repr(1+3j))
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
ValueError: complex() arg is a malformed string
 >>>



More information about the Python-list mailing list