鸭子打字 [英] Duck Typing

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

问题描述

Hello All,

我一直看到这个术语鸭子打字一段时间了。如果我们中的一个人可以用Python演示

鸭子打字和/或链接到某些Python参考文件,那么
会很好。


问候,

Srijit

Hello All,
I have been seeing this term "duck typing" for quite sometime now. It
will be nice if one of us can give an example in Python demonstrating
duck typing and/or link to some Python references.

Regards,
Srijit

推荐答案



< sr *** *@yahoo.com>在消息中写道

news:22 ************************** @ posting.google.c om ...

<sr****@yahoo.com> wrote in message
news:22**************************@posting.google.c om...
Hello All,
我一直在看这个术语鸭子打字一段时间了。如果我们中的一个人能够在Python中举例说明某种Python类型和/或某些Python引用的链接,那将会很好。


为什么世界上有人想做这样的事情?我没有听到这个词,直到你提起这个词,所以快速谷歌定义了它b
。 AFAICT,它只是一个可爱的名字,适用于一种非常常见的

技术,不会使用静态打字的语言。


换句话说,如果你需要一个类似文件的对象,并且有人通过

你是一个对象,你的选择要么只是使用它(并处理

例外)如果它不是真的支持正确的界面)或者使用

反射来查看它是否具有正确名称和数字的方法

参数,然后做好准备当他们没有做你期望的事情来处理例外情况时。


六分之一,六分之一。


John Roth

此致,
Srijit
Hello All,
I have been seeing this term "duck typing" for quite sometime now. It
will be nice if one of us can give an example in Python demonstrating
duck typing and/or link to some Python references.
Why in the world would anyone want to do such a thing? I hadn''t
heard the term until you just brought it up, so a quick Google defined
it. AFAICT, it''s simply a cute name that''s applied to a very common
technique in languages that don''t use static typing.

In other words, if you need a file-like object, and someone passes
you an object, your options are either to just use it (and handle the
exceptions if it doesn''t really support the proper interface) or use
reflection to see if it has methods with the proper name and number
of parameters, and then be prepared to handle the exceptions when
they don''t do what you expect.

Six of one, half a dozen of the other.

John Roth

Regards,
Srijit



sr **** @ yahoo.com 写道:
我一直看到这个术语鸭子打字一段时间了。如果我们中的一个人可以在Python中给出一个示例来演示单词和/或链接到一些Python引用,那将是很好的。
I have been seeing this term "duck typing" for quite sometime now. It
will be nice if one of us can give an example in Python demonstrating
duck typing and/or link to some Python references.




这有用吗: http://www.razorvine.net/python/PythonLanguageConcepts

(段落''动态打字'')?


- Irmen de Jong



Is this helpful: http://www.razorvine.net/python/PythonLanguageConcepts
(paragraph ''dynamic typing'') ?

--Irmen de Jong


在2003年9月16日星期二上午02:01:18 -0700, sr****@yahoo.com 写道:
On Tue, Sep 16, 2003 at 02:01:18AM -0700, sr****@yahoo.com wrote:
Hello All,
我一直在看这个术语鸭子打字一段时间了。如果我们中的一个人可以在Python中给出一个示例来演示单词和/或链接到一些Python引用,那将是很好的。
Hello All,
I have been seeing this term "duck typing" for quite sometime now. It
will be nice if one of us can give an example in Python demonstrating
duck typing and/or link to some Python references.




class Duck:

def quack(self):

print" Quack!"


class MeWearingSillyDuckOutfit:

def quack(self):

print" ummm ... Quack!"


def make_it_quack(obj):

obj.quack()


a = Duck()

b = MeWearingSillyDuckOutfit()

make_it_quack(a )

make_it_quack(b)



class Duck:
def quack(self):
print "Quack!"

class MeWearingSillyDuckOutfit:
def quack(self):
print "ummm... Quack!"

def make_it_quack(obj):
obj.quack()

a = Duck()
b = MeWearingSillyDuckOutfit()
make_it_quack(a)
make_it_quack(b)


这篇关于鸭子打字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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