Complex evaluation bug

Paul McGuire ptmcg at austin.rr._bogus_.com
Fri May 19 12:03:39 EDT 2006


"Christophe" <chris.cavalaria at free.fr> wrote in message
news:446d8209$0$7139$626a54ce at news.free.fr...
<snip>
> Oups, I was too fast to read what was written. I though you only changed
> one of the terms ( str -> repr ).
>
> You'll note that repr and str produce the exact same result for complex.
> And also, I'm not sure using eval anywhere is a good idea so it probably
> doesn't help for what the OP wants to do.

An eval-less approach - the problem is the enclosing parens.

>>> a = 1+3j
>>> str(a)
'(1+3j)'
>>> complex(str(a))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: complex() arg is a malformed string
>>> complex(str(a)[1:-1])
(1+3j)
>>> complex(str(a).strip("()"))
(1+3j)

-- Paul





More information about the Python-list mailing list