创建登录的简单命令行程序
raw_input输入用户名和密码
存储在字典中的用户名和密码
class UserRegistration(object):
username_and_password = {}
def __init__(self, username, password):
self.username = username
self.password = password
username = raw_input("Choose username>")
password = raw_input("Choose password>")
username_and_password[username] = password
此模块是否遵循惯例/是最佳做法?我该如何优化它?是否有更好的标准来创建登录/命令行登录?
答案 0 :(得分:0)
您不需要将username_and_password
设置为类变量,以便其他类无法访问它。变量username
和password
存在同样的问题。并且您不使用self.username/self.password
,因此无需分配它们。
同时,getpass()
也是一种优化。
所以我认为最好的做法是:
class UserRegistration(object):
def __init__(self):
self.username_and_password = {}
username = raw_input("Choose username>")
password = raw_input("Choose password>")
self.username_and_password[username] = password
编辑: 如果你想阅读dict的内容,你可以这样做:
myInfo = UserRegistration() # in this line, user will be required to enter username and password
username = raw_input('please enter username whose password you want to know: ')
print myInfo.username_and_password[username]