python 什么是鸭子类型
生活随笔
收集整理的這篇文章主要介紹了
python 什么是鸭子类型
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
什么是鴨子類型?
定義:如果走起路來像鴨子,叫起來也像鴨子,那么它就是鴨子(If it walks like a duck and quacks like a duck, it must be a duck)
鴨子類型是編程語言中動(dòng)態(tài)類型語言中的一種設(shè)計(jì)風(fēng)格,一個(gè)對(duì)象的特征不是由父類決定,而是通過對(duì)象的方法決定的。
?代碼如下
# -*- coding:utf8 -*- # /usr/bin/env pythonfrom collections import Iterable from collections import Iteratorclass b(str):passclass a(b):passclass MyIterator(a):def __iter__(self):passdef __next__(self):passprint(isinstance(MyIterator(), Iterable)) print(isinstance(MyIterator(), Iterator)) print(isinstance(MyIterator(), str))例如迭代器,我們并不需要繼承Iterable或者Iterator,只需要實(shí)現(xiàn)__iter__?和?__next__方法的對(duì)象都可稱之為迭代器,本身可以為任何類
轉(zhuǎn)載于:https://www.cnblogs.com/KK150713/p/11175850.html
總結(jié)
以上是生活随笔為你收集整理的python 什么是鸭子类型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三星android系统应用,三星Andr
- 下一篇: java 检查 死链接_如何检查网站死链