在Python中,如何检查我的类的实例是否存在? [英] In Python, how do I check if an instance of my class exists?

查看:222
本文介绍了在Python中,如何检查我的类的实例是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个程序,要求用户登录,我想知道如何使它,所以它检查是否存在我的用户名类的实例。

I'm making a program 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()


推荐答案

缺少的是保存您的 Usernames 实例的集合。对于这种特殊情况,您可能需要一个字典。

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'
>>> 

这篇关于在Python中,如何检查我的类的实例是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆