对象类型检查 [英] Object type check

查看:68
本文介绍了对象类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


在静态类型的语言中,让我们说C#,例如,我们通过接口使用

多态。所以我们用

方法M定义一个接口,然后用C语言实现I接口并为M方法编写代码



所以,如果我们有一个带有I类参数的函数,我们知道之前它将有一个M方法可以调用




但在动态语言中并非如此,我们可以通过我们想要的任何

来完成该功能。假设有人在其他程序员将使用的
Python中编写库,如果传递的参数是正确的,那么在该函数内检查
的正确方法是什么? />
类型,可能是isinstance BIF?


提前致谢!

Hi to all,

in statically-types languages, let''s say C# for example, we use
polymorphism through interfaces. So we define an interface I with
method M and then a class C that implements I interface and write code
for the M method.
So, if we have a function that takes a parameter of type I, we know
before-hand that it will have an M method to call.

But in dynamic languages this is not the case and we can pass whatever
we want to that function. Assuming that someone writes a library in
Python that other programmers will use, what is the correct way to
check inside that function if the parameter passed is of the correct
type, maybe "isinstance" BIF ?

Thanks in advance!

推荐答案

答案是什么都不做所有。使用您期望的对象的接口

。如果你期望它们可以像对待数字那样对待它们,或者对于b
stirngs或者iterables。调用您期望的方法和访问属性

。如果打电话通过了一些不好的事情,并且某些事情没有工作,那么他们会发现它并查阅你的文件,看看他们做错了什么。不要将它们限制为特定类型。你会将
限制在C#中的特定类。相反,您只需通过如何使用对象来定义

接口。这称为鸭子打字( http:// en。 wikipedia.org/wiki/Duck_typing )其中说如果它是一个像鸭子一样走路,它像鸭子一样嘎嘎叫,它必须是一只鸭子。在结束时,对你来说这不重要吗?不是一个对象的类型或定义的

接口,但是它符合你的期望。


2007年2月7日08:17:55 -0800, king kikapu< ab ******** @ panafonet.grwrote:
The answer is to do nothing at all. Use the interfaces of the objects
that you expect. Treat them like numbers if you expect them to be, or
stirngs, or iterables. Call methods and access attributes you expect
to be there. If the caller passes sometihng bad, and something doesn''t
work, they''ll find out about it and consult your documentation to see
what they did wrong. Dont restrict them to particular types. You would
not restrict them to a particular class in C#. Instead, you define the
interfaces simply by how you use the objects. This is called duck
typing (http://en.wikipedia.org/wiki/Duck_typing) which states "If it
walks like a duck and it quacks like a duck, it must be a duck." In
the end, isn''t that what matters to you? Not the type or defined
interfaces of an object, but that it does what you expect.

On 7 Feb 2007 08:17:55 -0800, king kikapu <ab********@panafonet.grwrote:

大家好,


在静态类型的语言中,让我们说C#,例如,我们通过接口使用

多态。所以我们用

方法M定义一个接口,然后用C语言实现I接口并为M方法编写代码



所以,如果我们有一个带有I类参数的函数,我们知道之前它将有一个M方法可以调用




但在动态语言中并非如此,我们可以通过我们想要的任何

来完成该功能。假设有人在其他程序员将使用的
Python中编写库,如果传递的参数是正确的,那么在该函数内检查
的正确方法是什么? />
类型,可能是isinstance BIF?


提前致谢!


-
http://mail.python.org/mailman/listinfo/python-list

< br $> b $ b -

阅读我的博客!我依靠你接受我的意见!我很有意思!
http://ironfroggy-code.blogspot.com/


2月7日下午5:17,king kikapu < aboudou ... @ panafonet.grwrote:
On Feb 7, 5:17 pm, "king kikapu" <aboudou...@panafonet.grwrote:

大家好,

静态类型语言的
,让比如说C#,我们通过接口使用

多态性。所以我们用

方法M定义一个接口,然后用C语言实现I接口并为M方法编写代码



所以,如果我们有一个带有I类参数的函数,我们知道之前它将有一个M方法可以调用




但在动态语言中并非如此,我们可以通过我们想要的任何

来完成该功能。假设有人在其他程序员将使用的
Python中编写库,如果传递的参数是正确的,那么在该函数内检查
的正确方法是什么? />
类型,可能是isinstance BIF?
Hi to all,

in statically-types languages, let''s say C# for example, we use
polymorphism through interfaces. So we define an interface I with
method M and then a class C that implements I interface and write code
for the M method.
So, if we have a function that takes a parameter of type I, we know
before-hand that it will have an M method to call.

But in dynamic languages this is not the case and we can pass whatever
we want to that function. Assuming that someone writes a library in
Python that other programmers will use, what is the correct way to
check inside that function if the parameter passed is of the correct
type, maybe "isinstance" BIF ?



通常,你根本不会检查任何东西。但是,在某些情况下,

可能会检查你是否通过了正确的对象。

例如,如果你有容器


class示例(对象):

def __init __(self,innerobj):

self.innerobj = innerobj
def amethod(self):

self.innerobj.amethod()


ex =示例(无)


只有在调用ex.amethod()时才会出错。如果你要在实例化后一小时打电话给

ex.amethod(),你会得到错误

迟到。 />
所以,检查__init__方法是有意义的,

就像


断言hasattr(self.innerobj,' 'amethod'')


,以便尽快收到错误消息。请注意

检查是否存在所需方法的
比使用isinstance更好,因为它给你更多

更多

innerobj的自由。不要求超过你需要的。

HTH,


Michele Simionato

Usually, you don''t check anything at all. However, in some cases, it
may make
sense to check that you passed the right object.
For instance if you have the container

class Example(object):
def __init__(self, innerobj):
self.innerobj = innerobj
def amethod(self):
self.innerobj.amethod()

ex = Example(None)

you will get an error only when calling ex.amethod(). If you are going
to call
ex.amethod() one hour after instantiation, you will get the error too
late.
So, it makes sense to put a check in the __init__ method, something
like

assert hasattr(self.innerobj, ''amethod'')

in order to get an error message as soon as possible. Notice that
checking for the existance
of the needed method is better than using isinstance, since it gives
you much more
freedom for innerobj. Do not require more than you need.
HTH,

Michele Simionato


不要将它们限制在特定类型。你需要
Dont restrict them to particular types. You would

不要将它们限制在C#中的特定类。相反,您只需通过如何使用对象来定义

接口。
not restrict them to a particular class in C#. Instead, you define the
interfaces simply by how you use the objects.



cource我将它们限制为特定类型!在C#中,你无法通过

这么糟糕的事情,因为编译器会抓住它!


我明白你的意思通过鸭子打字。所以你建议什么都不做

方向,

更好地记录我的代码,以便其他人可以看到预期的内容,对吧?

Of cource i restrict them to particular types! In C# you cannot pass
something bad
this way because the compiler will just catch it!

I see what you mean by "duck typing". So you suggest the "do nothing
at all" direction,
better document my code so other can see what is expected, right ?


这篇关于对象类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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