Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
方法名未定义错误,即使它是在python中定义的_Python_Class_Methods - Fatal编程技术网

方法名未定义错误,即使它是在python中定义的

方法名未定义错误,即使它是在python中定义的,python,class,methods,Python,Class,Methods,我已经定义了一个方法,并试图在构造函数中调用它,但是我得到的错误是“userDetailsValidation”没有定义,即使我在构造函数上面定义了它。这是我的密码: class PythonAssignment: FirstName = "" LastName = "" dateOfBirth = "" def userDetailsValidation(fieldvalue, fieldName, database): for entry i

我已经定义了一个方法,并试图在构造函数中调用它,但是我得到的错误是“userDetailsValidation”没有定义,即使我在构造函数上面定义了它。这是我的密码:

class PythonAssignment:
    FirstName = ""
    LastName = ""
    dateOfBirth = ""

    def userDetailsValidation(fieldvalue, fieldName, database):
        for entry in database:
            if fieldName in entry and entry[fieldName] == fieldvalue:
                return True

    def printRequiredUserInfo(FirstName, fieldname, AccountNumber, Accountbalance, Database):
        for entry in Database:
            if fieldname in entry and entry[fieldname] == FirstName:
                print(entry)

    def __init__(self):
        self.FirstName = str(input("Enter First Name").upper())
        while True:
            if (userDetailsValidation(FirstName, "FirstName", newSortedDatabase)) == True:
                userDetails.append(FirstName)
                break
            else:
                print(" First Name did not matched with the database")
                FirstName = str(input("Enter First Name").upper())


userObject = PythonAssignment()

您应该将不使用
self
的方法标记为静态

@staticmethod
def userDetailsValidation(fieldvalue, fieldName, database):

然后使用类作用域调用它们

if PythonAssignment.userDetailsValidation(FirstName, "FirstName", newSortedDatabase):
如果您的方法确实需要对象的状态,那么您应该从
self

self.SomeMethod(args, etc)

这里根本不需要使用类。你有使用OOP的具体原因吗?只是为了实践OOP的概念。除了我在下面写的内容外,你还应该阅读关于你误用它们的内容。好的,谢谢你的解决方案。@RohnKerry如果重点是学习OO,那么就用正确的方法来做吧。。。一个类不能代替一个模块。这解决了你的问题吗?在你看来,我被名字难住了。未定义FirstName。你能帮我吗?不,正如我提到的,你还有其他问题。您的
FirstName
等不应在类级别上,它们应仅在
\uuuu init\uuuuuu
中定义,并且是
self
的一个属性,例如
self.FirstName
。然后,任何非静态的类方法都应该将
self
作为第一个参数,然后它们可以访问任何成员变量
self.SomeMethod(args, etc)