__get__,__set__

2020-04-13  本文已影响0人  huashen_9126

=赋值的时候,调用某些逻辑

class Base(object):
    def __set__(self, instance, value):
        print('====>set')
        self.ret = value + ' de ' + instance.name + ' shi tou zhu'

    def __get__(self, instance, owner):
        print('====>get')
        return self.ret

    def __delete__(self, instance):
        print("====>delete")
        print("delete " + instance.name)

class Test(object):
    n = Base()

    def __init__(self, name):
        self.name = name
    
    def test(self):
        self.n = 'huahua'
        print(self.n)

    def delete(self):
        del self.n


t = Test('name')
t.test()
t.delete()
#结果
====>set
====>get
huahua de name shi tou zhu
====>delete
delete name

扩展__delete__del的时候触发
实际应用:自动化测试脚本的某些操作用=赋值的方式实现,如:输入,选择

上一篇 下一篇

猜你喜欢

热点阅读