function call behaves differently in a class vs w/o class in python3
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")
开发者_StackOverflow enterParam(msg) #<==== not defined
if not checkStr(value): #<====not defined
print("incorrect symbols detected, try again")
enterParam(msg) #<====not defined
return value
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.
精彩评论