__slots__的使用

class Student(object):
    __slots__=('name','age','set_age','score')#restrict the attribute of   the class

s=Student()
s.name='NNN' #bind a attribution
print(s.name)


def  set_age(self,age):
    self.age=age
from types import MethodType
s.set_age=MethodType(set_age,s)#bind a method
s.set_age(33)
print(s.age)

def set_score(self,score):
    self.score=score
Student.set_score=set_score

t=Student()
t.set_score(333)
#print(t.score)

class GraduateStudent(Student):
    __slots__=('name','score')
    pass
g=GraduateStudent()
g.score=33
print(g.score)

你可能感兴趣的:(__slots__的使用)