class User_cls_info(object):
def __init__(self, name, age, gender, hobby, ):
self.__name = name
self.__age = age
self.__gander = gender
self.__hobby = hobby
# 查看
def check_info(self):
for i in self.__dict__:
v = getattr(self, i)
i_1 = i.split('__')[1]
print(f'数据名:{i_1},数据值:{v}')
# 修改
def alter_info(self):
alter_k = input('请输入您需要修改的数据名>>>:').strip()
alter_v = input('请输入修改后的数据值>>>:').strip()
alter_k_ = f'_User_cls_info__{alter_k}'
if alter_k_ not in self.__dict__:
print('该数据名不存在')
return
if len(alter_v) <= 0:
print('数据值不得为空')
return
setattr(self, alter_k_, alter_v)
print(f'修改成功,{alter_k},已被修改为{alter_v}')
# 增加
def add_info(self):
add_k = input('请输入您需要增加的数据名>>>:').strip()
add_v = input('请输入对应的数据值>>>:').strip()
if len(add_k) <= 0 or len(add_v) <= 0:
print('数据值或数据名不得为空')
return
add_k_ = f'_User_cls_info__{add_k}'
setattr(self, add_k_, add_v)
print(f'已成功添加,数据名:{add_k},数据值:{add_v}')
# 删除
def del_info(self):
del_k = input('请输入您需要删除的数据>>>:').strip()
del_k = f'_User_cls_info__{del_k}'
if del_k not in self.__dict__:
print('该数据名不存在')
return
delattr(self, del_k)
print(f'数据{del_k}已删除')
def run(self):
while True:
print('''
1.查看用户信息>>>:check_info
2.修改用户信息>>>:alter_info
3.增加用户信息>>>:add_info
4.删除用户信息>>>:del_info
''')
user_choice = input('请输入您需要执行的功能>>>:').strip()
if hasattr(self, user_choice):
getattr(self, user_choice)()
else:
print('该功能不存在')
user = User_cls_info('kangkang', 18, 'male', 'read')
user.run()
标签:info,__,.__,PYthon,self,改查,strip,增删,alter
From: https://www.cnblogs.com/kangssssh/p/16867551.html