python动态绑定的特性

python作为一种脚本语言,无需编译,解释执行,在使用python的过程中运用了动态绑定的这一特性,故此总结一下;

先看一段代码:

dynamic.py

class People(object):
    def test(self):
        print "this is a function in class"


def test():
    print "this is a test function"
p = People()
p.test = test
p.test()
p.name = "Jack"


boy = People()
boy.test()
print p.name
print dir(People)

执行这个脚本python dynamic.py,输出结果为:

this is a test function
this is a function in class
Jack
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'test']

上面的程序就说明了这一点,man对象在最初的时候有一个test方法,但在之后被外面的一个function重写了,故此man对象的方法也改变了,但原有的类的方法没有变化,从boy对象就可以看出来。。。。

看起来好像是面向对象语言中的继承,这里的动态加载和继承的意义不同,动态加载的内容并没有作为该类的一个属性或者方法,只是临时绑定到一个对象上,或者对对象的原有属性或者方法进行了重写;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值