Python-动态绑定属性和方法
1 class Student: 2 def __init__(self, name, age): 3 self.name = name 4 self.age = age 5 def eat(self): 6 print(self.name+'在吃饭') 7 8 stu1 = Student('张三', 20) 9 stu2 = Student('李四', 30) 10 print(id(stu1)) 11 print(id(stu2)) 12 print('-----为stu1绑定性别属性-----') 13 stu1.gender = '女' 14 print(stu1.name, stu1.age, stu1.gender) 15 print(stu2.name, stu2.age) 16 Student.gender = '男' 17 print(stu1.name, stu1.age, stu1.gender) 18 print(stu2.name, stu2.age, stu2.gender) 19 20 stu1.eat() 21 stu2.eat() 22 23 print('-----为stu1绑定show方法-----') 24 def show(): 25 print('定义在类之外的,成为函数') 26 stu1.show = show 27 stu1.show()