[py]python多态-动态语言的鸭子类型
弱类型?强类型?动态语言,静态语言
弱类型: 在程序运行过程中,类型可变

还有一种说法:
动态
variables must necessarily be defined before they are used. But the good thing is that these variables need not be declared, and they need not be bound to a particular type. Python is a very good example of a dynamic typed programming language.



- 弱类型:
> "1"+2
'12'
强类型:
>>> "1"+2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
动态类型:
>>> a = 1
>>> type(a)
<type 'int'>
>>> a = "s"
>>> type(a)
<type 'str'>
静态类型:
Prelude> let a = "123" :: Int
<interactive>:2:9:
Couldn't match expected type `Int' with actual type `[Char]'
In the expression: "123" :: Int
In an equation for `a': a = "123" :: Int
鸭子类型的典故
只要拥有动物的talk方法,就可以去使用meeting方法. 而不必一定要是anaimal的子类, 总得找个合适的比喻来描述吧. 像是鸭子,会叫,就把他当成鸭子, 像是狗会狗叫,就当他是狗, 只不过我们一般用鸭子来比喻.

“A bird that walks like a duck and swims like a duck and quacks like a duck, is a duck.”
The actual Apparel of an entity doesn't matter if the entity does all the intended things.

In other words, don't check whether it IS-A duck,check whether it QUACKS-like-a duck, WALKS-like-a duck, etc. depending on exactly what subset of duck-like behavior you need for your program.
class Duck:
def quack(self):
print "這鴨子在呱呱叫"
def feathers(self):
print "這鴨子擁有白色與灰色羽毛"
class Person:
def quack(self):
print "這人正在模仿鴨子"
def feathers(self):
print "這人在地上拿起1根羽毛然後給其他人看"
def in_the_forest(duck):
duck.quack()
duck.feathers()
def game():
donald = Duck()
john = Person()
in_the_forest(donald)
in_the_forest(john)
game()
[py]python多态-动态语言的鸭子类型的更多相关文章
- 010_动态语言与鸭子类型及python2和3的区别
一. 动态语言中经常提到鸭子类型,所谓鸭子类型就是:如果走起路来像鸭子,叫起来也像鸭子,那么它就是鸭子(If it walks like a duck and quacks like a duck, ...
- python语言的鸭子类型和强类型语言的多态
python语言的鸭子类型和强类型语言的多态 前面讲接口类的时候举过一个有关支付方式的例子,支付方式可以有几种,微信支付,支付宝支付,苹果支付等,这几个不同的支付都统一于支付,像这样几个类都统一于 某 ...
- Python:多态、协议和鸭子类型
多态 问起面向对象的三大特性,几乎每个人都能对答如流:封装.继承.多态.今天我们就要来说一说 Python 中的多态. 所谓多态:就是指一个类实例的相同方法在不同情形有不同表现形式.多态机制使具有不同 ...
- Python的动态语言特性; __slots__属性
python是动态语言 1. 动态语言的定义 动态编程语言 是 高级程序设计语言 的一个类别,在计算机科学领域已被广泛应用.它是一类 在运行时可以改变其结构的语言 :例如新的函数.对象.甚至代码可以被 ...
- Python 类的属性和实例属性 Python 的动态语言
#定义一下类, 含有类属性 aaa, >>> class Aa: ... aaa = 10... #对类 Aa实例化出一个实例 a1>>> a1 = Aa() &g ...
- 第7.3节 Python特色的面向对象设计:协议、多态及鸭子类型
Python是一种多态语言,其表现特征是:对象方法的调用方只管方法是否可调用,不管对象是什么类型,从而屏蔽不同类型对象之间的差异,写出通用的代码,做出通用的编程,以适应需求的不断变化. 一. P ...
- Python笔记(三)继承和多态、动态语言
一.继承 先定义一个A类 class A(object): def fun(self): print "Run A fun()" 在定义一个B类 class B(A): pass ...
- python之类的多态(鸭子类型 )、封装和内置函数property
一.多态 1.什么是多态:一个类表现出的多种状态--->通过继承来实现的例如:class Animal:passclass Dog(Animal):passclass Cat(Animal):p ...
- python与鸭子类型
部分参考来源:作者:JasonDing https://www.jianshu.com/p/650485b78d11##s1 首先介绍下面向对象(OOP)的三大特征: (1)面向对象程序设计有三大特 ...
随机推荐
- Android自定义字体
1.在assets中创建文件夹fonts,放入字体文件.ttf 2.使用实例: TextView tv= (TextView)findViewById(R.id.tv); Typeface tf = ...
- ldap命令
ldapadd -x 进行简单认证 -D 用来绑定服务器的DN -h 目录服务的地址 -w 绑定DN的密码 -f 使用ldif文件进行条目添加的文件 -W 交互式输入DN的密码 ...
- Android开发训练之第五章第四节——Syncing to the Cloud
Syncing to the Cloud GET STARTED DEPENDENCIES AND PREREQUISITES Android 2.2 (API level 8) and higher ...
- Esper学习之十四:Pattern(一)
1. Pattern Atoms and Pattern operatorsPattern是通过原子事件和操作符组合在一起构成模板.原子事件有3类,操作符有4类,具体如下: 原子事件:1). 普通事件 ...
- 金蝶KIS问题解决汇总
1.帐套结转时,提示t_subsys插入重复键 解决: I.删除索引 alter table t_subsys drop constraint pk_subsys II.t_rp_initial表 ...
- 【多线程系列】AQS CAS简单介绍
一.什么是CAS CAS(Compare And Swap),即比较并交换.是解决多线程并行情况下使用锁造成性能损耗的一种机制,CAS操作包含三个操作数——内存位置(V).预期原值(A)和新值(B). ...
- php curl-class post
use \Curl\Curl; $curl = new Curl();$curl->setHeader('Content-Type', 'application/json');$curl-> ...
- 部署OpenStack问题汇总(五)--openstack中删除虚拟主机,状态一直未deleting
[原创文章,转载请注明出处] 一.我重启了该机器,之后想删除没有创建成功的虚拟机(没有打开cpu的vt),结果发现状态一直为deleting状态.在这个状态下创建虚拟机也失败. 二.分析:在/var/ ...
- linux常用命令大全3--rpm安装软件
RPM 包 - (Fedora, Redhat,CentOS及类似系统) rpm -ivh package.rpm 安装一个rpm包 rpm -ivh --nodeeps package.rpm 安装 ...
- Mac下门罗币矿工样本分析
背景 今天遇到一个JSONRPC的告警,怀疑挖矿木马,IOC是132.148.245.101,无其他信息,随即google一波. 查询网络 遇到了,主动下载样本分析,下载地址:http://rjj.q ...