How to check arguments of the function?

前端 未结 3 1876
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 00:23

I have function defined this way:

def f1 (a, b, c = None, d = None):
    .....

How do I check that a, b are not equal

相关标签:
3条回答
  • 2021-01-25 00:47

    Why can't you refer to the values by their names?

    def f1 (a, b, c=None, d=None):
        if not a.strip():
            print('a is not empty')
    

    If you have many arguments it is worth changing function signature to:

    def f2 (*args, c=None, d=None):
        for var in args:
            if not var.strip():
                raise ValueError('all elements should be non-empty')
    
    0 讨论(0)
  • 2021-01-25 00:51

    A typical approach would be:

    import sys
    
    ...
    
    def check_attribute(name, value):
        """Gives warnings on stderr if the value is an empty or whitespace string.
    
           All other values, including None, are OK and give no warning.
        """
        if isinstance(value, basestring) and (not value or value.isspace()):
            print>>sys.stderr, "Invalid value %r for argument %r" % (value, name)
    

    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...):

    def check_arguments(d):
        for name, value in d.iteritems():
            check_attribute(name, value)
    

    and the function would be just:

    def f1 (a, b, c=None, d=None):
        check_arguments(locals())
        ...
    

    You could, alternatively, write a decorator in order to be able to code

    @checked_arguments
    def f1 (a, b, c=None, d=None):
       ...
    

    (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_arguments function I defined above):

    import functools
    import inspect
    
    def checked_arguments(f):
      @functools.wraps(f)
      def wrapper(*a, **k):
        d = inspect.getcallargs(f, *a, **k)
        check_arguments(d)
        return f(*a, **k)
      return wrapper
    

    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 from www.python.org and 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).

    0 讨论(0)
  • 2021-01-25 00:55
    for key, value in locals().items():
        if value is not None:
            check_attribute(key, value)
    

    Though as others have said already, you can just check the arguments directly by name.

    0 讨论(0)
提交回复
热议问题