天天看点

对象修改属性查询获取

# 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'}
    """      
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)