Discuss / Python / Test

菜问

#1 Created at ... [Delete] [Delete and Lock User]
#对象动态绑定

class test(object):
    pass

t1 test()
t1.name = "n1"

t2 test()
print(t2.name) #错误,对象动态绑定,仅对所绑定的对象有效,对其它对象无效
#类的动态绑定

class test(object):
    pass

test.name = "n1"

t1 test()
t2 test()
print(t1.name) #输出n1
print(t2.name) #输出n1
#限制绑定

class test(object):
    __slots__('name',)

test.name = "n1" # ok
test.age  = 123 # 错误,只能绑定name属性

t1 test()
t1.name = "n1" # ok
t1.age  = 123 # 错误,只能绑定name属性

  • 1

Reply