复杂的评估错误 [英] Complex evaluation bug

查看:76
本文介绍了复杂的评估错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a = 1 + 3j

复合体(str(a))


为什么这不起作用?它应该

解决方案

我不确定发生了什么,但我收到了错误:


ValueError:complex()arg是一个格式错误的字符串


我认为这可能是因为''j''的值没有定义。


但我是新手,所以我很可能错了。


Brian Blazer
br *** @ brianandkate.com


2006年5月18日上午11:36,写道:
< blockquote class =post_quotes> a = 1 + 3j
复杂(str(a))

为什么这不起作用?它应该
-
http:// mail.python.org/mailman/listinfo/python-list




写道:

a = 1 + 3j
复杂(str(a))

为什么这不起作用?它应该



说谁?


通过Python中的常规约定,str尝试只制作一个漂亮的b $ b人类可读表示。功能repr通常需要

来提供可以解析回原始对象的输出。

(虽然对于数字复合类型,两者产生相同的结果。)


此外,构造函数很少需要解析字符串

表示来返回一个对象。函数eval通常

可以提供这种功能。


所以,把它们放在一起,你可以期待

eval(repr(a ))

重现a,事实上它是这样的。


加里赫伦

PY> c =复杂(1,3)

py>打印c

(1 + 3j)

py> d =复杂(''1 + 3j'')

py>打印d

(1 + 3j)

py> str(1 + 3j)

''(1 + 3j)''

complex需要两个数字,或一个表示复数的字符串。

你提供的字符串不代表有效的复数。


a = 1+3j
complex(str(a))

Why does this not work ? It should

解决方案

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.


这篇关于复杂的评估错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆