函数调用在python3中的类vs w / o类中表现不同

时间:2011-04-20 16:44:39

标签: python-3.x

这是我的代码:

#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

现在我的问题是: 这在脚本的正文中工作正常,但是当我放入类似下面的类时,eclipse / pydev开始抱怨enterParam和checkStr没有被定义。我做错了什么?

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

1 个答案:

答案 0 :(得分:4)

您需要将方法称为self.enterParam()self.checkStr()

(另请注意,Python style guide PEP 8建议命名enter_param()check_str()等方法 - CamelCase仅用于Python中的类名。