Discuss / Python / set_age是属性还是方法?slot不是限制属性的么?

set_age是属性还是方法?slot不是限制属性的么?

Topic source

下面代码是可以运行的,但是问题是,我认为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)

redglass2015

#2 Created at ... [Delete] [Delete and Lock User]

方法名也是变量,应该也属于属性吧


  • 1

Reply