a = 1+3j
complex(str(a))
Why does this not work ? It should 11 3054
I am not exactly sure what is going on, but I get the error:
ValueError: complex() arg is a malformed string
I think that it might be because the value of 'j' is not defined.
But I am a newbie so I could very well be wrong.
Brian Blazer br***@brianandkate.com
On May 18, 2006, at 11:36 AM, of wrote: a = 1+3j complex(str(a))
Why does this not work ? It should -- http://mail.python.org/mailman/listinfo/python-list
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
py> c = complex(1, 3)
py> print c
(1+3j)
py> d = complex('1+3j')
py> print d
(1+3j)
py> str(1+3j)
'(1+3j)'
complex takes two numbers, or a string representing a complex number.
the string you supply isn't a representation of valid complex number.
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.
eval (str(1+3j)) works.
Python 2.4.2 (#1, Jan 23 2006, 21:24:54) [GCC 3.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more
information. 1+3j
(1+3j) str(1+3j)
'(1+3j)' complex (str(1+3j))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: complex() arg is a malformed string eval (str(1+3j))
(1+3j) eval(str(1+3j))**2
(-8+6j)
"Mel Wilson" <mw********@sympatico.ca> wrote in message
news:Yk*******************@news20.bellglobal.com.. . 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
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
Christophe wrote: 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 >>>
in what language is "eval" spelled "complex" ?
</F>
Fredrik Lundh a écrit : Christophe wrote:
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 >>>
in what language is "eval" spelled "complex" ?
</F>
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.
"Christophe" <ch*************@free.fr> wrote in message
news:44**********************@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
Am Freitag 19 Mai 2006 18:03 schrieb Paul McGuire: An eval-less approach - the problem is the enclosing parens. <snip>
I've just submitted two patches to the Python bugtracker at: http://sourceforge.net/tracker/index...70&atid=305470
which either change the repr() format (removing the parentheses), which I find
doubtful, because it's not backwards-compatible, or alter the constructor to
accept the repr() format for complex numbers (a bracketed number).
Feel free to comment.
--- Heiko.
Heiko Wundram wrote: Am Freitag 19 Mai 2006 18:03 schrieb Paul McGuire:
An eval-less approach - the problem is the enclosing parens. <snip>
I've just submitted two patches to the Python bugtracker at:
http://sourceforge.net/tracker/index...70&atid=305470
which either change the repr() format (removing the parentheses), which I find doubtful, because it's not backwards-compatible, or alter the constructor to accept the repr() format for complex numbers (a bracketed number).
Feel free to comment.
--- Heiko.
thanks This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bhushit Joshipura |
last post by:
This post contains one question and one proposal.
A. May I know why order of evaluation of arguments is not specified in
C/C++?
I asked a question in comp.lang.c++ for the following...
|
by: dragoncoder |
last post by:
Consider the following code.
#include <stdio.h>
int main()
{
int i =1;
printf("%d ,%d ,%d\n",i,++i,i++);
return 0;
}
|
by: berns |
last post by:
Hi All,
A coworker and I have been debating the 'correct' expectation of
evaluation for the phrase a = b = c. Two different versions of GCC
ended up compiling this as b = c; a = b and the other...
|
by: Random |
last post by:
I have a very full and complex object that contains objects as properties
and a few collections, and I'm working to databind it to a page. A lot of
textbox and dropdowns on the page, a few grids. ...
|
by: silpau |
last post by:
hi,
i am a bit confused on expression evaluation order in expressions
involving unary increment.decrement operators along with binary
operators.
For example in the following expression
x...
|
by: Rasjid |
last post by:
Hello,
I have just joined and this is my first post.
I have never been able to resolve the issue of order of evaluation in
C/C++ and the related issue of precedence of operators, use of...
|
by: Pietro Cerutti |
last post by:
Hi group,
here I come with a question which is quite simple per se, but for which
I can't find an answer.
Does the C standard guarantee that inside an expression such as
(x && y)
"y" is...
|
by: Boltar |
last post by:
Why does C do lazy evaluation for logical boolean operations but not
bitwise ones?
Ie: the following program prints "1 2" , not "1 1" under gcc
main()
{
int a = 1;
int b = 1;
0 && ++a;
|
by: Peter Graf |
last post by:
Hi,
i tried to evaluate the 2F1(a,b,c,z)
hypergeometric function for a=1, b=2-0.1i,
c=2.5-0.01i and z=-5e8. I've used the numerical
recipes routine but this routine couldn't act with
this...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |