Complex evaluation bug

Gary Herron gherron at islandtraining.com
Thu May 18 18:59:04 CEST 2006


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.

Gary Herron




More information about the Python-list mailing list