• 对象修改属性查询获取


    # coding=utf-8
    ALL_VARS = {}
    
    
    def set_var(key, value):
        ALL_VARS[key] = value
    
    
    def get_var(key):
        return ALL_VARS.get(key, "not exist this key")
    
    
    class T(object):
    
        def __init__(self):
            self.name = "ssss"
            self.age = 11111
    
    
    if __name__ == '__main__':
    
        t = T()
        setattr(t, "name", "zhansgs")
        # change attr 
        t.__setattr__("age", 999)
        # add attr
        t.__setattr__("keys","values")
        # get attr 
        print(t.name, t.age,t.__getattribute__("keys"),t.__dict__)
       
        """
        ###### output:
        zhansgs
        999
        values
        {'name': 'zhansgs', 'age': 999, 'keys': 'values'}
        """
    

      2.关于成员方法变量获取:

    import inspect
    class F(object):

    def __init__(self):
    self.name = 'A'

    def hello(self,name:str,age:int):
    print('hello',name,age)

    def getmethod(self):
    return(list(filter(lambda m: not m.startswith("__")
    and not m.endswith("__") and
    callable(getattr(self, m)), dir(self))))


    def hello(name:str,age:int,js=999):
    name=111
    age=999
    return None


    print(F().getmethod())
    print(F().__dict__)
    print(inspect.getfullargspec(hello).args[1:])
    """
    ['getmethod', 'hello']
    {'name': 'A'}
    ['age', 'js']
    """
    #
    # t2 = __import__("get_specialvar")
    # result = inspect.getmembers(t2,inspect.isclass)
    # result2 = inspect.getmembers(t2,inspect.isfunction)
    # result3 = inspect.getmembers(t2,inspect.ismodule)
    # result4 = inspect.getmembers(t2,inspect.ismethod)
    # result5 = inspect.getmembers(t2,inspect.isgeneratorfunction)
    # result6 = inspect.getmembers(t2,inspect.isbuiltin)
    # print (result)
    # print (result2)
  • 相关阅读:
    容器镜像加速器配置(Windows)
    容器镜像加速器配置(CentOS)
    kubernetes部署基于kubeadmin部署单机版本
    k8s之calico部署
    PHP连接SQLSERVER及中文乱码问题
    通过css grid实现圣杯布局
    sequelize操作数据库
    vue 导出excel中的多个sheet
    caffe python 接口api
    linux tmux 详细教程,Linux中Tmux的安装和基本用法
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/11296824.html
Copyright © 2020-2023  润新知