2011-04-20 58 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