convenientclass person@rangetest(percent=( )use decorator to validate def giveraise(selfpercent)self pay int(self pay ( percent)isolating validation logic in decorator simplifies both clients and future maintenance notice that our goal here is different than the attribute validations coded in the prior final example herewe mean to validate the values of function arguments when passedrather than attribute values when set python' decorator and introspection tools allow us to code this new task just as easily basic range-testing decorator for positional arguments let' start with basic range test implementation to keep things simplewe'll begin by coding decorator that works only for positional arguments and assumes they always appear at the same position in every callthey cannot be passed by keyword nameand we don' support additional **args keywords in calls because this can invalidate the positions declared in the decorator code the following in file called rangetest pydef rangetest(*argchecks)validate positional arg ranges def ondecorator(func)if not __debug__true if "python - main py args return func no-opcall original directly elseelse wrapper while debugging def oncall(*args)for (ixlowhighin argchecksif args[ixhigherrmsg 'argument % not in % % (ixlowhighraise typeerror(errmsgreturn func(*argsreturn oncall return ondecorator as isthis code is mostly rehash of the coding patterns we explored earlierwe use decorator argumentsnested scopes for state retentionand so on we also use nested def statements to ensure that this works for both simple functions and methodsas we learned earlier when used for class' methodoncall receives the subject class' instance in the first item in *args and passes this along to self in the original method functionargument numbers in range tests start at in this casenot new herenotice this code' use of the __debug__ built-in variable--python sets this to trueunless it' being run with the - optimize command-line flag ( python - main pywhen __debug__ is falsethe decorator returns the origin function unchangedto avoid extra later calls and their associated performance penalty in other wordsthe decorator automatically removes its augmentation logic when - is usedwithout requiring you to physically remove the decoration lines in your code examplevalidating function arguments