下面代码是可以运行的,但是问题是,我认为set_age是方法,赋值给属性age的一种方法,那应该不需要用slot来豁免set_age才对啊?可是如果没有就会抛no attr 'set_age'的error...
class Student(object): __slots__ = ('name', 'age', 'set_age') pass def set_age(self, age): self.age=age from types import MethodType s1 = Student() s1.set_age=MethodType(set_age, s1) s1.set_age(20) print(s1.age)
方法名也是变量,应该也属于属性吧
Sign in to make a reply
飞奔的兔毛
下面代码是可以运行的,但是问题是,我认为set_age是方法,赋值给属性age的一种方法,那应该不需要用slot来豁免set_age才对啊?可是如果没有就会抛no attr 'set_age'的error...