2011-04-20 40 views
0

这里是我的代码:函数调用中表现不同类VS W/O在python3类

#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 

现在我的问题是: 此作品的脚本体内OK,但只要我放入类如下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 

回答

4

您需要调用的方法为self.enterParam()self.checkStr()

(另请注意,Python style guide PEP 8建议名称的方法,如enter_param()check_str() - 驼峰仅用于在Python类名

+0

这很简单 - 非常感谢!;)从Java的到来,我忘了那。 – 2011-04-20 16:50:03

+1

有关此行为的解释,请参阅http://stackoverflow.com/a/5467009/821378。此外,如果您不需要访问自己,通常最好将这些功能保留在模块级别,您不必使它们成为方法。 – 2012-04-02 11:02:38

相关问题