Oct-03-2019, 10:32 AM
(This post was last modified: Oct-03-2019, 10:33 AM by burningkrome.)
This is an opinion question.
When I code, I generally use assert statements, @property, or casting to check, error out, and/or correct improper parameters passed in. However, some feel this is bloat and I should just let bad parameters error out to force the calling developer to correct her/his code.
Which is the more pythonic method? Fix it or let it crash?
I.e.
When I code, I generally use assert statements, @property, or casting to check, error out, and/or correct improper parameters passed in. However, some feel this is bloat and I should just let bad parameters error out to force the calling developer to correct her/his code.
Which is the more pythonic method? Fix it or let it crash?
I.e.
def testMethod(intParam, stringCharParam): try: intParam = int(intParam) if ((intParam < 1) or (intParam > 10)): raise ValueError() except: raise ValueError("The parameter intParam must be an integer between 1 and 10. [intParam = {}]".format(str(intParam)) ) log.debug("intParam = {}".format(str(intParam))) stringCharParam = ''.join(c for c in str(stringCharParam) if re.match("[a-zA-Z]", c)) assert len(stringCharParam) > 0 , "The parameter stringCharParam must be a string containing only characters (I.e. A-Z,a-z ). [stringCharParam = {}]".format(str(stringCharParam)) log.debug("stringCharParam = {}".format(stringCharParam )) print(intParam, stringCharParam)