0

I have defined one method and I tried to call it inside a constructor, However I am getting error "userDetailsValidation" is not defined, Even If I have defined it above the constructor. Here is my code:

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()
AKX
  • 152,115
  • 15
  • 115
  • 172
Rohn Kerry
  • 155
  • 1
  • 4
  • 15

1 Answers1

1

You should label your methods that don't use self as static

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

and

@staticmethod
def printRequiredUserInfo(FirstName,fieldname,AccountNumber,Accountbalance,Database):

then to call them, you use the class scope

if PythonAssignment.userDetailsValidation(FirstName, "FirstName", newSortedDatabase):

If your method do need the state of the object, then you should call them from self

self.SomeMethod(args, etc)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Does this resolved the problem at your end, I am stuck at FirstName. FirstName is not defined. Can you help me with this? – Rohn Kerry Jul 27 '18 at 11:37
  • No as I mentioned you have other problems as well. Your `FirstName`, etc should not be up at the class level, they should be defined in `__init__` only and be an attribute of `self`, e.g. `self.FirstName`. Then any of your class methods that aren't static should take `self` as the first argument, then they can access any member variables – Cory Kramer Jul 27 '18 at 11:39