我已经定义了一个方法,并尝试在构造函数内调用它,但是,即使在构造函数上方定义了“ 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()
答案 0 :(得分:1)
您应该将不使用self
的方法标记为静态
@staticmethod
def userDetailsValidation(fieldvalue, fieldName, database):
和
@staticmethod
def printRequiredUserInfo(FirstName,fieldname,AccountNumber,Accountbalance,Database):
然后调用它们,使用类范围
if PythonAssignment.userDetailsValidation(FirstName, "FirstName", newSortedDatabase):
如果您的方法确实需要对象的状态,则应从self
self.SomeMethod(args, etc)