# 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‘}
"""
原文:https://www.cnblogs.com/SunshineKimi/p/11296824.html