"Mel Wilson" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> of wrote:
> > a = 1+3j
> > complex(str(a))
> >
> > Why does this not work ? It should
>
> It would be nice.
> Looks like str(1+3j) is returning an expression in string
> form.  Maybe there is no actual complex literal.
>

The problem is that str(1+3j) emits a string enclosed in parens.  Stripping
them off makes the string acceptable to complex's constructor, without
invoking eval.

>>> 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


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to