zoukankan      html  css  js  c++  java
  • 对象修改属性查询获取

    # 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)
  • 相关阅读:
    Django学习笔记之Cookie、Session和自定义分页
    sass表达式前后出现空格
    render总结
    vue双向绑定补充说明方法
    mutation与action
    keep-alive使用笔记
    this指向 一般函数与箭头函数
    vue-router原理分析
    history新增方法
    常用阻止ajax缓存方法集锦
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/11296824.html
Copyright © 2011-2022 走看看