Python动态语言的体现

2018-01-05  本文已影响0人  yeathMe

Python作为一门动态语言 ,他的动态性体现在可以动态给对象添加属性 和方法。

1,通过TypeMethod方法可以给对象动态添加实例属性,比如说实现在某一个方法被调用的时候自动传入一个instance作为self参数

2, 可以动态给类添加方法

@staticmethod
def test():
   print "--------test_----"
@classmethod
def printNum():
   print "class num"
class Person(object):
   def __init___(self,newName, newAge):
       self.name=newName
       self.age=newAge
Person .xy=test
person.xy()
Person .classNum=printNum
person.classNum



上一篇 下一篇

猜你喜欢

热点阅读