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)

image

posted @ 2022-11-09 20:29  *鱼之乐  阅读(30)  评论(0)    收藏  举报