失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > [转]编程语言中的 鸭子模型(duck typing)

[转]编程语言中的 鸭子模型(duck typing)

时间:2021-07-29 22:56:56

相关推荐

[转]编程语言中的 鸭子模型(duck typing)

在学习Python的时候发现了鸭子类型这个术语,听起来好像很有意思一样,所以把它记下来。

鸭子类型的核心概念在于一个对象的有效语义,不是继承自特定的类或者实现特定的方法,而是

由当前的属性和方法集合决定的。听起来依旧很迷茫,简单的说:

"当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为鸭子。"

在鸭子类型中,关注的不是对象的类型本身,而是它是如何使用的。

实际的例子1,将前两个参数相加后和第三个参数相乘:

1 def add_then_multiplication(a, b, c):2return (a + b) * c3 4 print add_then_multiplication(1, 2, 3)5 print add_then_multiplication([1, 2, 3], [4, 5, 6], 2)6 print add_then_multiplication('hello', 'world', 9)

只要参数之间支持+和*运算,就在不使用继承的情况下实现多态。

例子2,鸭子和人:

1 class Duck: 2def quack(self): 3 print "Quaaaaack..." 4def feathers(self): 5 print "The Duck has feathers." 6 7 class Person: 8def quack(self): 9 print "Person is not a duck"10def feathers(self):11 print "The person takes a feather from the ground and shows it."12 13 def in_the_forest(duck):14duck.quack()15duck.feathers()16 17 def game():18donald = Duck()19john = Person()20in_the_forest(donald)21in_the_forest(john)22 23 game()

这个例子中,可以看出来,人不是鸭子,但是一旦拥有的鸭子的一些方法,就可以被当成--鸭子类型。

"在Python中,鸭子类型的最典型例子就是类似file的类。这些类可以实现file的一些或全部方法,并可以用于file通常使用的地方。" 但是一旦一个类不具有鸭子的方法,大概是要出错的。如:

1 class Duck: 2def quack(self): 3 print "Quaaaaack..." 4def feathers(self): 5 print "The Duck has feathers." 6 7 class Person: 8def quack(self): 9 print "Person is not a duck"10def feathers(self):11 print "The person takes a feather from the ground and shows it."12 13 class Car:14def run(self):15 print "Car is running"16 17 def in_the_forest(duck):18duck.quack()19duck.feathers()20 21 def game():22donald = Duck()23john = Person()24in_the_forest(donald)25in_the_forest(john)26car = Car()27try: # will throw exception28 in_the_forest(car)29except (AttributeError, TypeError):30 print "No the method for duck typing"31 32 game()33

"在更实际的实现类似file的行为的例子中,人们更倾向于使用Python的异常处理机制来处理各种各样的可能因为各种程序员无法控制的环境和operating system问题而发生的I/O错误。在这里,“鸭子类型”产生的异常可以在它们自己的子句中捕获,与操作系统、I/O和其他可能的错误分别处理,从而避开复杂的检测和错误检查逻辑。"

个人认为:鸭子类型也许能够避免一些类的重写,无需大量复制相同的代码,但是需要良好的文档支持,不然会让代码变得很混乱,谁知道你的鸭子是不是我的鹅呢?哈哈

来自维基百科: 鸭子类型

如果觉得《[转]编程语言中的 鸭子模型(duck typing)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。