0

Here is my code:

#Check if the value has only allowed characters
def checkStr(value):
         return (set(value) <= allowed)
#Enter parameter
def enterParam (msg)
   value=input(msg)
   if len(value) == 0:
      print("Cannot be empty, try again")
      enterParam(msg)
   if not checkStr(value):
      print("incorrect symbols detected, try again")
      enterParam(msg)
   return value

Now my question is: This works OK inside the body of the script, but as soon as i put inside a class like below the eclipse/pydev starts complaining about enterParam and checkStr not being defined. What am i doing wrong?

class ParamInput:
    #Check if the value has only allowed characters
    def checkStr(self, value):
             return (set(value) <= allowed)
    #Enter parameter
    def enterParam (self, msg)
       value=input(msg)
       if len(value) == 0:
          print("Cannot be empty, try again")
          enterParam(msg) #<==== not defined
       if not checkStr(value): #<====not defined
          print("incorrect symbols detected, try again")
          enterParam(msg) #<====not defined
       return value
Eugene Sajine
  • 8,104
  • 3
  • 23
  • 28

1 Answers1

4

You need to call the methods as self.enterParam() and self.checkStr().

(Also note that the Python style guide PEP 8 recommends to name methods like enter_param() and check_str() -- CamelCase is only used for class names in Python.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • That was easy - thanks a lot!;) Coming from java, i forgot about that. – Eugene Sajine Apr 20 '11 at 16:50
  • 1
    See http://stackoverflow.com/a/5467009/821378 for an explanation of this behavior. Also, if you don’t need to access self it’s usually best to keep the functions at the module level, you don’t have to make them methods. – merwok Apr 02 '12 at 11:02