用于测试函数类型的足够pythonic代码 [英] sufficiently pythonic code for testing type of function

查看:60
本文介绍了用于测试函数类型的足够pythonic代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为其他人写了这个以获取对象和类型列表,

然后检查obj是否是其中一种类型,否则会引发错误。


是否足以依赖副作用或缺席副作用,或者我应该在这里将
返回True吗?


def test_obj_type(obj,类型):

类型类型:

if isinstance(obj,type):

break

else :

提高ValueError,''对象不在%s''%类型


- Theerasak

解决方案

Theerasak Photha写道:


我写这篇文章给别人拿一个对象和类型列表,

然后检查obj是否属于这些类型之一,否则会引发错误。


是否足以依赖副作用或缺少副作用,或者我应该

把这里的返回True设为什么?


def test_obj_type(obj,types):

类型类型:

if isinstance(obj,type):

break

else:

raise ValueError ,''对象不在%s''%类型



你好Theerasak,


回答你的问题:要么(a)如果没有则返回True,如果不是则返回错误

或(b)使其成为断言:如果不正确则引发异常,如果确定,请执行

无效。从上面的函数中返回True将是一个相当不错的b $ b奇怪的混合体。


但是:


1.如果isinstance(obj,types [1]为true,但isinstance(obj,types [0])

为false,这似乎会引发ValueError。是否为
$ b $的缩进b其他并提高你的意图吗?


2.无论如何,从Python 2.2开始,不需要循环:


def test_obj_type (obj,types):

如果不是isinstance(obj,types):

引发ValueError,''对象不在%s''%(types,)


如果您不想要断言风格,您的其他人可以直接致电

isinstance。


3.请注意加注线的变化;如果类型是两个或更多项目的元组,那么%运算符会特别对待它。如编码,你

会得到这个例外:

" TypeError:并非在字符串格式化期间转换所有参数"


HTH,

John


Theerasak Photha写道:


我写这个给别人拿一个对象和类型列表,

然后检查obj是否是其中一种类型,


这已经是isinstance(obj,tuple_of_types)的作用。


否则会引发错误。


是否足以依赖副作用或不存在副作用,或者我应该在这里将
返回True?



为什么?


def test_obj_type(obj,types):

类型类型:

if isinstance(obj,type):

break

else:

raise ValueError,''对象不在%s''%types




如果不是isinstance(obj,types):

引发ValueError(''对象不是%s的实例''%str(类型))

现在真正的问题:如果对象不是任何类型的实例,但仍然支持预期的界面怎么办?


- Theerasak



-

bruno desthuilliers

python -c" print''@''。join([ ''。''。加入([w [:: - 1] for p in p.split(''。'')])for

p in''o **** @ xiludom .gro''。split(''@'')])"


< blockquote>在10/11/06,Bruno Desthuilliers< on *** @ xiludom.growrote:


现在真正的问题:如果对象不是

类型的实例,但仍然支持预期的接口?



也许:


尝试:

for [''foo''中的属性, ''bar'',''_ baz__'']:

getattr(mystery_object,''__%s__''%属性)

除了AttributeError:

#总结一下吧


重新加注特定应用程序的异常

详细信息是否错误条款?


- Theerasak


I wrote this for someone else to take an object and list of types,
then check if obj is one of those types, raising an error otherwise.

Is it enough to rely on side effects or absence thereof, or should I
put return True in here somewhere?

def test_obj_type(obj, types):
for type in types:
if isinstance(obj, type):
break
else:
raise ValueError, ''object is not in %s'' % types

-- Theerasak

解决方案

Theerasak Photha wrote:

I wrote this for someone else to take an object and list of types,
then check if obj is one of those types, raising an error otherwise.

Is it enough to rely on side effects or absence thereof, or should I
put return True in here somewhere?

def test_obj_type(obj, types):
for type in types:
if isinstance(obj, type):
break
else:
raise ValueError, ''object is not in %s'' % types

Hello Theerasak,

To answer your question: Either (a) return True if OK, False if not OK
or (b) make it like an assertion: raise an exception if not OK, do
nothing if OK. Returning True from the above function would be a rather
strange hybrid.

However:

1. if isinstance(obj, types[1] is true, but isinstance(obj, types[0])
is false, this would appear to raise ValueError. Is the indentation of
the else and raise what you intended?

2. In any case, since Python 2.2, no loop is necessary:

def test_obj_type(obj, types):
if not isinstance(obj, types):
raise ValueError, ''object is not in %s'' % (types, )

If you don''t want the assertion style, your "someone else" can call
isinstance directly.

3. And please notice the change in the raise line; if types is a tuple
of two or more items, the % operator treats it specially. As coded, you
would get this exception:
"TypeError: not all arguments converted during string formatting"

HTH,
John


Theerasak Photha wrote:

I wrote this for someone else to take an object and list of types,
then check if obj is one of those types,

This is already what isinstance(obj, tuple_of_types) does.

raising an error otherwise.

Is it enough to rely on side effects or absence thereof, or should I
put return True in here somewhere?

What for ?

def test_obj_type(obj, types):
for type in types:
if isinstance(obj, type):
break
else:
raise ValueError, ''object is not in %s'' % types



def checkinstanceof(obj, types):
if not isinstance(obj, types):
raise ValueError(''object is not an instance of %s'' % str(types))
Now the real question : what if the object is not an instance of any of
the types, but still support the expected interface ?

-- Theerasak


--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


On 10/11/06, Bruno Desthuilliers <on***@xiludom.growrote:

Now the real question : what if the object is not an instance of any of
the types, but still support the expected interface ?

Perhaps:

try:
for attribute in [''foo'', ''bar'', ''__baz__'']:
getattr(mystery_object, ''__%s__'' % attribute)
except AttributeError:
# Do sumthin bout it

Is it wrong to ''re-raise'' an exception with application-specific
details within an except clause?

-- Theerasak


这篇关于用于测试函数类型的足够pythonic代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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