• python之attr


    面向对象之attr

    class Foo(object):
        item = 123
    
        def __setattr__(self, key, value):
            print(key, value)
    
        def __getattr__(self, item):
            print(item)
    
    
    obj = Foo()
    obj.x = 123        #x 123
    print(obj.item)    #123
    

    ※attr的应用

    class Local(object):
        def __init__(self):
            # 初始化 得到self.storage = {},每次执行类的__setattr__方法,而不会覆盖之前的键值对
            object.__setattr__(self, "storage", {})
    
        def __setattr__(self, key, value):
            self.storage[key] = value
    
        def __getattr__(self, key):
            return self.storage.get(key)
    
    
    local = Local()     # 实例化local对象,执行__init__方法,执行object.__setattr__的方法:self.storage = {}
    local.x1 = 123      # 自动执行__setattr__方法,self.storage = {"x1": 123}
    print(local.x1)     # 自动执行__getattr__方法,取字典的值,得到 123
    local.x2 = 456      # 自动执行__setattr__方法,self.storage = {"x2": 456}
    print(local.x2)     # 456
    

    自定义简单版local

        import threading
    """
    storage = {
        1111:{'x1':0},
        1112:{'x1':1}
        1113:{'x1':2}
        1114:{'x1':3}
        1115:{'x1':4}
    }
    """
    class Local(object):
        def __init__(self):
            object.__setattr__(self,'storage',{})
    
        def __setattr__(self, key, value):
            ident = threading.get_ident()
            if ident in self.storage:
                self.storage[ident][key] = value
            else:
                self.storage[ident] = {key:value}
    
        def __getattr__(self, item):
            ident = threading.get_ident()
            if ident not in self.storage:
                return
            return self.storage[ident].get(item)
    
    local = Local()
    
    def task(arg):
        local.x1 = arg
        print(local.x1)
    
    for i in range(5):
        t = threading.Thread(target=task,args=(i,))
        t.start()
    执行结果
    0
    1
    2
    3
    4
    

    加强版local

    import threading
    
    """
    storage = {
        1111:{'x1':[]},
        1112:{'x1':[]}
        1113:{'x1':[]}
        1114:{'x1':[]}
        1115:{'x1':[]},
        1116:{'x1':[]}
    }
    """
    
    
    class Local(object):
        def __init__(self):
            object.__setattr__(self, 'storage', {})
    
        def __setattr__(self, key, value):
            ident = threading.get_ident()
            if ident in self.storage:
                self.storage[ident][key].append(value)
            else:
                self.storage[ident] = {key: [value, ]}
    
        def __getattr__(self, item):
            ident = threading.get_ident()
            if ident not in self.storage:
                return
            return self.storage[ident][item][-1]
    
    
    local = Local()
    
    
    def task(arg):
        local.x1 = arg
        print(local.x1)
    
    
    for i in range(5):
        t = threading.Thread(target=task, args=(i,))
        t.start()
    希望你眼眸有星辰,心中有山海,从此以梦为马,不负韶华
  • 相关阅读:
    MongoDB mongod.log "connection refused because too many open connections" 处理方法
    12C RAC 故障分析
    Linux(Redhat 7.6)安装PG(Postgresql 9.6.0)
    OGG异构平台安装部署 MySQL->Oracle
    Redhat 7安装Oracle 11.2.0.4 RAC 数据库软件中报错:Error in invoking target 'agent nmhs' of makefile
    Redhat 7.6安装11G RAC GI时遇到此类报错
    Oracle 19.3 RAC on Redhat 7.6 安装最佳实践
    RAC环境中某数据文件(非system表空间)创建在本地,不停机迁移到ASM磁盘中
    python 多线程
    go博客大全
  • 原文地址:https://www.cnblogs.com/daviddd/p/11914443.html
Copyright © 2020-2023  润新知