对于培训,我想在使用类的系统中创造性地创建用户。
这是我的代码:
class CsrUser(object):
def __init__(self, f_name, l_name, login, role, sex, password ='Hot12345'):
self.f_name = f_name
self.l_name = l_name
self.login = login
self.role = role
self.sex = sex
self.password = password
我想从用户输入
创建类的动态对象#i want to create something like this
def get_users_data():
new_user = input("Enter the user name")
... #get all the data
new_user = CsrUser(...)
我希望对象的名称是new_user
中的值答案 0 :(得分:5)
我不认为使用变量命名变量(在您的情况下为对象)是个好主意。如果您想按名称跟踪所有用户,则应使用字典:
#make dictionary
user_data = {}
#make a new user object
new_user = CsrUser(...)
#insert your new user object into the dictionary
#use new_user.f_name or new_user.l_name here in place of new_user.name (or combine both)
user_data[new_user.name] = new_user
#to get a user object out of the dictionary
a_user = user_data["name_here"]