注册 登录
编程论坛 Python论坛

请教duck type的详细信息

Yimmm 发布于 2012-03-20 17:55, 1565 次点击
我是学C#的,老师突然问道关于Python的一片外文中的duck type结果半天没有答上来。忘大神不吝赐教
1 回复
#2
为Jay沉沦2012-04-12 20:59
Duck typing

也许不翻译是最容易理解的了

definition: duck typing is a humorous way of describing the type non-checking system. initially coined by Dave Thomas in the Ruby community, its premise is that (referring to a value) "if it walks like a duck, and talks like a duck, then it is a duck."

 

Even though Boo is a statically typed language, Duck Typing is a way to fake being a dynamic language. Duck typing allows variables to be recognized at runtime, instead of compile time. Though this can add a sense of simplicity, it does remove a large security barrier.

e.g.

d as duck

d = 5 // 现在d是一个整型

print d

d += 100 // 能够做任何整型允许的操作

d = "hello" // 现在d变成了字符串类型了

print d

d = d.ToUpper() // 可以执行任何字符串操作了

print d
希望对你有用
1