In Python, how do I check if an instance of my class exists?
I'm making a program 开发者_StackOverflow中文版that asks the user to login, and I'm wondering how I can make it so it checks if an instance of my Username class exists. Oh and please excuse my sloppy and disorganized coding, I'm not very good at it.
quit_login = 1
class Usernames:
def __init__(self, password):
self.password = password
testlogin = Usernames("foo")
def login_e():
a = raw_input("Please enter a username: ")
new_pass = ""
if isinstance(a, Usernames):
a = Usernames(new_pass)
print Usernames
else:
login_pass = raw_input("What is your password?\n")
if login_pass == a.password:
print "Hello", a
else:
print "Incorrect password"
while quit_login != 0:
login_e()
The missing piece is a collection to hold your Usernames
instances. For this particular scenario, you probably want a dictionary.
>>> myDict = {}
>>> myDict['foo'] = 5
>>> 'foo' in myDict
True
>>> myDict['foo']
5
>>> myDict.get('bar', 'nope')
'nope'
>>>
精彩评论