complex I/O problem
If I call "print" on a complex value, I may get this: '(2+2j)' But this is not acceptable as input: complex ('(2+2j)') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: complex() arg is a malformed string Whatever format is used for output should be accepted as input!
On Tue, Feb 01, 2005 at 11:11:37AM -0500, Neal Becker wrote:
complex ('(2+2j)') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: complex() arg is a malformed string
Whatever format is used for output should be accepted as input!
This isn't true in general; it's not true of strings, for example, nor of files. Parsing complex numbers would be pretty complicated, because it would have to accept '(2+2j)', '2+2j', '3e-6j', and perhaps even '4j+3'. It seems easier to just use eval() than to make complex() implement an entire mini-parser. --amk
On Tue, 1 Feb 2005 12:16:10 -0500, A.M. Kuchling <amk@amk.ca> wrote:
On Tue, Feb 01, 2005 at 11:11:37AM -0500, Neal Becker wrote:
complex ('(2+2j)') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: complex() arg is a malformed string
Whatever format is used for output should be accepted as input!
This isn't true in general; it's not true of strings, for example, nor of files. Parsing complex numbers would be pretty complicated, because it would have to accept '(2+2j)', '2+2j', '3e-6j', and perhaps even '4j+3'. It seems easier to just use eval() than to make complex() implement an entire mini-parser.
Well, complex('2+2j') works, so it's not that far... But the rules are different: - There's no requirement whatsoever for str(); it can be whatever makes the most sense for the type. - For repr(), if at all possible, eval(repr(x)) == x should hold, in a suitable environment (you may have to import certain things in the namespace). If this can't be made true, repr(x) should be of the form <...>. - If there's no need for str() and repr() to be different, let str(x) == repr(x). So I think complex() is just fine. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
"A.M. Kuchling" <amk@amk.ca> wrote:
On Tue, Feb 01, 2005 at 11:11:37AM -0500, Neal Becker wrote:
complex ('(2+2j)') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: complex() arg is a malformed string
Whatever format is used for output should be accepted as input!
This isn't true in general; it's not true of strings, for example, nor of files. Parsing complex numbers would be pretty complicated, because it would have to accept '(2+2j)', '2+2j', '3e-6j', and perhaps even '4j+3'. It seems easier to just use eval() than to make complex() implement an entire mini-parser.
Which brings up the fact that while some things are able to make the eval(str(obj)) loop, more are able to make the eval(repr(obj)) loop (like strings themselves...). - Josiah
participants (4)
-
A.M. Kuchling -
Guido van Rossum -
Josiah Carlson -
Neal Becker