Python动态绑定属性和方法
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def study(self):
print(self.name + '在学习')
stu1 = Student("张三", 12)
stu2 = Student('小明', 15)
print('stu1的id:', id(stu1))
print('stu2的id:', id(stu2))
print('----------------------')
stu1.study()
stu2.study()
print('----------------------')
# 给两个实例对象绑定新的gender属性
stu1.gender = '男'
stu2.gender = '女'
print(stu1.name, stu1.age, stu1.gender)
print(stu2.name, stu2.age, stu2.gender)


浙公网安备 33010602011771号