弱类型?强类型?动态语言,静态语言

弱类型: 在程序运行过程中,类型可变

还有一种说法:

动态

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多态-动态语言的鸭子类型的更多相关文章

  1. 010_动态语言与鸭子类型及python2和3的区别

    一. 动态语言中经常提到鸭子类型,所谓鸭子类型就是:如果走起路来像鸭子,叫起来也像鸭子,那么它就是鸭子(If it walks like a duck and quacks like a duck, ...

  2. python语言的鸭子类型和强类型语言的多态

    python语言的鸭子类型和强类型语言的多态 前面讲接口类的时候举过一个有关支付方式的例子,支付方式可以有几种,微信支付,支付宝支付,苹果支付等,这几个不同的支付都统一于支付,像这样几个类都统一于 某 ...

  3. Python:多态、协议和鸭子类型

    多态 问起面向对象的三大特性,几乎每个人都能对答如流:封装.继承.多态.今天我们就要来说一说 Python 中的多态. 所谓多态:就是指一个类实例的相同方法在不同情形有不同表现形式.多态机制使具有不同 ...

  4. Python的动态语言特性; __slots__属性

    python是动态语言 1. 动态语言的定义 动态编程语言 是 高级程序设计语言 的一个类别,在计算机科学领域已被广泛应用.它是一类 在运行时可以改变其结构的语言 :例如新的函数.对象.甚至代码可以被 ...

  5. Python 类的属性和实例属性 Python 的动态语言

    #定义一下类, 含有类属性 aaa, >>> class Aa: ... aaa = 10... #对类 Aa实例化出一个实例 a1>>> a1 = Aa() &g ...

  6. 第7.3节 Python特色的面向对象设计:协议、多态及鸭子类型

    Python是一种多态语言,其表现特征是:对象方法的调用方只管方法是否可调用,不管对象是什么类型,从而屏蔽不同类型对象之间的差异,写出通用的代码,做出通用的编程,以适应需求的不断变化. 一.    P ...

  7. Python笔记(三)继承和多态、动态语言

    一.继承 先定义一个A类 class A(object): def fun(self): print "Run A fun()" 在定义一个B类 class B(A): pass ...

  8. python之类的多态(鸭子类型 )、封装和内置函数property

    一.多态 1.什么是多态:一个类表现出的多种状态--->通过继承来实现的例如:class Animal:passclass Dog(Animal):passclass Cat(Animal):p ...

  9. python与鸭子类型

    部分参考来源:作者:JasonDing  https://www.jianshu.com/p/650485b78d11##s1 首先介绍下面向对象(OOP)的三大特征: (1)面向对象程序设计有三大特 ...

随机推荐

  1. 【Python3】 PyV8的安装与使用

    centos7  python3.6.4 1 安装boost  :  PyV8 依赖于Boost yum install boost yum install boost-devel yum insta ...

  2. Java 反编译工具 —— JAD 的下载地址(Windows版/Linux版/Mac OS 版)

    Java 反编译工具 —— JAD 的下载地址. 各种版本哦! Windows版,Linux版,Mac OS 版,等等 下载地址: http://varaneckas.com/jad/

  3. eagle学习汇总

    一.原理图编辑器 1. 编辑->全局属性->可以设置全局变量,选择“文本框”,以‘>’开头代表引用全局属性的值. 2. 绘制->Frame->可绘制原理图边框,一般选择“ ...

  4. mysql的root的权限被控制无法授权

    一.环境: MariaDB [(none)]> select version(); +----------------+ | version()      | +---------------- ...

  5. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

  6. [原]openstack-kilo--issue(二) openstack auth error

    /** 系统环境:redhat7.2 repo:163 openstack version : kilo author: lihaibo 本博客已经添加"打赏"功能,"打 ...

  7. 使用sql语句删除数据库中重复的数据

    在数据库中有一批数据,但是由于数据入库之前某些不当的操作引入了一些重复的数据,需要将这些重复的数据删除.数据的结构大概是下面的样子 CREATE TABLE [dbo].[aaa]( [id] [in ...

  8. Linux大文件已删除,使用df查看已使用的空间并未减少

    今天在一台Linux系统里,使用df命令查看磁盘使用情况,发现/根分区使用了100% 然后删除一大文件/var/lib/pgsql/9.1/pgstartup.log,再使用df查看使用情况,显示内容 ...

  9. 【CF815D】Karen and Cards 单调栈+扫描线

    [CF815D]Karen and Cards 题意:一张卡片有三个属性a,b,c,其上限分别为A,B,C,现在有n张卡片,定义一张卡片能打败另一张卡片当且仅当它的至少两项属性要严格大于另一张的对应属 ...

  10. [转]Shell脚本之无限循环的两种方法

    方法一: while循环,用的比较多的 #!/bin/bash set j= while true do let "j=j+1" echo "----------j is ...