参数是可迭代的吗? [英] is parameter an iterable?

查看:172
本文介绍了参数是可迭代的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个参与的功能。我的代码需要这个参数

是一个可迭代的(我可以循环的东西)...所以我不在乎它是否是一个

列表,元组等等所以我需要一种方法来确保参数在使用之前是可迭代的。我知道我能做到......


def foo(inputVal):

if isinstance(inputVal,(list,tuple)):
输入值为val的


#做东西


....但我想覆盖任何迭代,因为我只需要循环

it。


有什么建议吗?

解决方案

2005年11月15日11:01:48 -0800,

" py" <共******* @ gmail.com>写道:

我有一个参数的函数。我的代码需要这个参数
是一个可迭代的(我可以循环的东西)...所以我不在乎它是否是一个列表,元组等等所以我需要一种方法来确保在使用它之前,参数是可迭代的。我知道我可以做...
def foo(inputVal):
如果isinstance(inputVal,(list,tuple)):
对于inputVal中的val:
#do stuff
...但是我想覆盖任何迭代,因为我只需要循环它。
有什么建议吗?




就这么做吧。如果foo的一个调用者传入一个不可迭代的,foo会

引发异常,你会在测试期间捕获它。请注意

字符串,但是:

def foo(i):
... for j in i:

... print j foo([1,3,4,5,6,7])
1

3

4

5

$

foo(" hello")



h

e

l

l

o


问候,

Dan


-

Dan Sommers

< http://www.tombstonezero.net/dan/>


星期二,2005-11-15 11:01 - 0800, py写道:

我有一个参数的函数。我的代码需要这个参数
是一个可迭代的(我可以循环的东西)...所以我不在乎它是否是一个列表,元组等等所以我需要一种方法来确保在使用它之前,参数是可迭代的。我知道我能做到......

def foo(inputVal):
如果isinstance(inputVal,(list,tuple)):
对于inputVal中的val:
#do stuff

...但是我想覆盖任何可迭代的,因为我只需要循环
它。

任何建议?




如果hasattr(inputVal,''__ getitem__'')

<你可能可以侥幸逃脱


br />

Dan Sommers写道:

就这么做。如果foo的一个调用者传入一个不可迭代的,foo将会引发异常,你会在测试期间发现它




这正是我不想要的。我不想要一个例外,而是我想要检查它是否是可迭代的....如果它是继续的话,如果不是

返回错误代码。我在测试期间无法捕捉它,因为这是其他人将使用的



感谢您的建议。


I have function which takes an argument. My code needs that argument
to be an iterable (something i can loop over)...so I dont care if its a
list, tuple, etc. So I need a way to make sure that the argument is an
iterable before using it. I know I could do...

def foo(inputVal):
if isinstance(inputVal, (list, tuple)):
for val in inputVal:
# do stuff

....however I want to cover any iterable since i just need to loop over
it.

any suggestions?

解决方案

On 15 Nov 2005 11:01:48 -0800,
"py" <co*******@gmail.com> wrote:

I have function which takes an argument. My code needs that argument
to be an iterable (something i can loop over)...so I dont care if its a
list, tuple, etc. So I need a way to make sure that the argument is an
iterable before using it. I know I could do... def foo(inputVal):
if isinstance(inputVal, (list, tuple)):
for val in inputVal:
# do stuff ...however I want to cover any iterable since i just need to loop over
it. any suggestions?



Just do it. If one of foo''s callers passes in a non-iterable, foo will
raise an exception, and you''ll catch it during testing. Watch out for
strings, though:

def foo(i): ... for j in i:
... print j foo([1, 3, 4, 5, 6, 7]) 1
3
4
5
6
7 foo("hello")


h
e
l
l
o

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>


On Tue, 2005-11-15 at 11:01 -0800, py wrote:

I have function which takes an argument. My code needs that argument
to be an iterable (something i can loop over)...so I dont care if its a
list, tuple, etc. So I need a way to make sure that the argument is an
iterable before using it. I know I could do...

def foo(inputVal):
if isinstance(inputVal, (list, tuple)):
for val in inputVal:
# do stuff

...however I want to cover any iterable since i just need to loop over
it.

any suggestions?



You could probably get away with

if hasattr(inputVal, ''__getitem__'')


Dan Sommers wrote:

Just do it. If one of foo''s callers passes in a non-iterable, foo will
raise an exception, and you''ll catch it during testing



That''s exactly what I don''t want. I don''t want an exception, instead I
want to check to see if it''s an iterable....if it is continue, if not
return an error code. I can''t catch it during testing since this is
going to be used by other people.

Thanks for the suggestion though.


这篇关于参数是可迭代的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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