对象修改属性查询获取

# 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)

转载于:https://www.cnblogs.com/SunshineKimi/p/11296824.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值