无法为每个用户创建一个错误列表

时间:2019-07-17 18:39:43

标签: python-3.x oop

我创建了一个名为Users()的类,我想进行的验证之一是密码验证,例如最小密码长度,至少包含一个数字,依此类推。当我实例化两个用户并调用方法check_password时,问题就出现了,当我打印结果时,它只显示一个列出了两个用户错误的列表,而不是每个用户一个。下面的代码。

user.py:

users = []
class Users:
    error_list = []

    def __init__(self, user_id, user_name, password = '1234'):
        self.__user_id = user_id
        self.__user_name = user_name
        self.__password = password
        users.append({"user_id":self.user_id,"user_name":self.__user_name,"password":self.__password})

    @property
    def user_id(self):
        return self.__user_id

    @property
    def user_name(self):
        return self.__user_name

    @property
    def password(self):
        return self.__password

    def check_password(self):
        if len(self.__password) < 8:
            self.error_list.append({'error_cd': 1,'error_message':'your password must have at least 8 characters'})
            return self.error_list

main.py:

from user import Users

user1 = Users(1,'Johnny','111')
user2 = Users(2,'Carlos','11111')

print(user1.password)
print(user1.check_password())

print(user2.password)
print(user2.check_password())

执行main.py的输出是:

"C:\Users\a0091660\Desktop\Meus arquivos\Pessoal\pyProjects\Pluralsight\roleta_russa\venv\Scripts\python.exe" "C:/Users/a0091660/Desktop/Meus arquivos/Pessoal/pyProjects/Pluralsight/roleta_russa/main.py"
111
[{'error_cd': 1, 'error_message': 'your password must have at least 8 characters'}]
11111
[{'error_cd': 1, 'error_message': 'your password must have at least 8 characters'}, {'error_cd': 1, 'error_message': 'your password must have at least 8 characters'}]

Process finished with exit code 0

我想:

"C:\Users\a0091660\Desktop\Meus arquivos\Pessoal\pyProjects\Pluralsight\roleta_russa\venv\Scripts\python.exe" "C:/Users/a0091660/Desktop/Meus arquivos/Pessoal/pyProjects/Pluralsight/roleta_russa/main.py"
111
[{'error_cd': 1, 'error_message': 'your password must have at least 8 characters'}]
11111
[{'error_cd': 1, 'error_message': 'your password must have at least 8 characters'}]

Process finished with exit code 0

0 个答案:

没有答案