I have function defined this way:
def f1 (a, b, c = None, d = None):
.....
How do I check that a, b are not equal to some value. E.g. I want to check they are not empty strings like, "" or " "
Thinking about something like.
arguments = locals()
for item in arguments:
check_attribute(item, arguments[item])
And then check if arguments are not "", " ". But in this case it will also try to check None values (what I don’t want to do).
A typical approach would be:
or, of course, you could issue warnings, or raise exceptions if the problem is very serious according to your application’s semantics.
One should probably delegate all of the checking to a single function, instead of looping in the function whose args you’re checking (the latter would be sticking “checking code” smack in the middle of application logic — better keep it out or the way…):
and the function would be just:
You could, alternatively, write a decorator in order to be able to code
(to get checking code even more “out of the way”), but this might be considered overkill unless you really have a lot of functions requiring exactly this kind of checks!
Argument-name introspection (while feasible, thanks to module
inspect) is far less simple in a decorator than within the function itself, which is why my favorite design approach would be to eschew the decorator approach in this case (simplicity is seriously good;-).Edit — showing how to implement a decorator, since the OP explicitly asked for one (though without clarifying why).
The main problem (in Python 2.6 and earlier) is for the wrapper to construct a mapping equivalent to the
locals()which Python makes for you, but needs to be done explicitly in a generic wrapper.But — if you use the new 2.7, inspect.getcallargs does it for you! So, the problem becomes much simpler, and the decorator perhaps worth doing in many more cases (if you’re in 2.6 or earlier, I still recommend eschewing the decorator approach, which would be substantially more complicated, for such specialized uses).
So, here is all you need, in Python 2.7 (reusing the
check_argumentsfunction I defined above):The difficulty in pre-2.7 versions comes entirely from the difficulty of implementing the equivalent of
inspect.getcallargs— so, I hope that, if you really need decorators of this kind, you can simply download Python 2.7 fromwww.python.organd install it on your box!-)(If you do, you’ll also get many more goodies besides, as well as a longer support cycle than just about any previous Python version, since 2.7 is slated to be the last release in the Python
2.*line).