1.动态给实例绑定属性:
class Student(object):
pass
s=Student()
s.name='xiaoming'
print s.name =====> 输出:xiaoming
通过导入types的MethodType方法实现
def set_age(self,age):
self.age = age
s.set_age(8) #执行该语句时,系统会报错,因为类中无set_age()方法。
----------------------------------------解决方法-------------------------------------------------------------------------
from types import MethodType
s.set_age = MethodType(set_age,s)
s.set_age(25)
s.age =======>输出25
def set_score(self,score):
self.score = score
Student.set_score = set_score #类名.方法名=方法名
s1 = Student()
s1.set_score(100)
s1.score ===============================>输出100