class People(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __setitem__(self, key, value):
self.__dict__[key] = value
def __getitem__(self, item):
return self.__dict__[item]
handsome = People("靓仔", 18)
# print(handsome.name)
handsome["age"] = 20 # 调用__setitem__方法,修改对象属性
handsome["name"] = "beefcake"
print(handsome.age)
print(handsome.name)
print("-" * 20)
print(handsome["age"]) # 调用__getitem__方法,调用对象属性
print(handsome["name"])
print("-" * 20)
print(handsome.__dict__) # 将对象转化为字典