为什么我会得到一个TypeEror? [英] Why would I get a TypeEror?
问题描述
对于此代码剪辑:
a = 3
.....
b =(1,len(a ))[isinstance(a,(list,tuple,dict))]
为什么我会从len函数中得到一个TypeError?
谢谢,
For this code snip:
a=3
.....
b=(1,len(a))[isinstance(a,(list,tuple,dict))]
Why would I get a TypeError from the len function?
Thanks,
推荐答案
这是我写的:
对于这段代码剪辑:
a = 3
....
b =(1,len(a))[isinstance(a,(list,tuple,dict))]
为什么我会从len函数中得到一个TypeError吗?
For this code snip:
a=3
....
b=(1,len(a))[isinstance(a,(list,tuple,dict))]
Why would I get a TypeError from the len function?
您对长度的期望是什么?整数3是?
-Peter
What did you expect the "length" of the integer 3 to be?
-Peter
2005年1月12日,在18:35,这是我写的:
On 12.01.2005, at 18:35, It''s me wrote:
对于这段代码剪辑:
a = 3
.... > b =(1,len(a))[isinstance(a,(list,tuple,dict))]
为什么我会从len函数中获取TypeError?
For this code snip:
a=3
....
b=(1,len(a))[isinstance(a,(list,tuple,dict))]
Why would I get a TypeError from the len function?
因为len()仅适用于序列和映射对象:
because len() works only for sequence and mapping objects:
help(len)
模块内置函数len的帮助__builtin__:
len(。 ..)
len(对象) - >整数
返回序列或映射的项目数。
- harold -
-
Ceci n''est pas une une signature。
-
Help on built-in function len in module __builtin__:
len(...)
len(object) -> integer
Return the number of items of a sequence or mapping.
- harold -
--
Ceci n''est pas une signature.
--
2005年1月12日18:35,我写道:
On 12.01.2005, at 18:35, It''s me wrote:
对于这段代码剪辑:
= 3
....
b =(1,len(a))[isinstance(a,(list,tuple,dict))]
为什么我会得到来自len函数的TypeError?
For this code snip:
a=3
....
b=(1,len(a))[isinstance(a,(list,tuple,dict))]
Why would I get a TypeError from the len function?
问题是,(1,len(a))被评估,不是什么类型的
实际上有
(python没有像ML这样的内置惰性评估)。你必须这样做
方式
代替:
a = 3
... 。
b = isinstance(a,(list,tuple,dict))和len(a)或1
- harold -
-
正确陈述的反面是虚假陈述。
但是,与深刻真相相反的可能是另一个深刻的事实。 />
- Niels Bohr
the problem is, that (1,len(a)) is evaluated, neither what type a
actually has
(python has no builtin lazy evaluation like ML). You have to do it this
way
instead:
a=3
....
b = isinstance(a,(list,tuple,dict)) and len(a) or 1
- harold -
--
The opposite of a correct statement is a false statement.
But the opposite of a profound truth may be another profound truth.
-- Niels Bohr
这篇关于为什么我会得到一个TypeEror?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!