Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 910979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:07:36+00:00 2026-05-15T17:07:36+00:00

I have function defined this way: def f1 (a, b, c = None, d

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:07:37+00:00Added an answer on May 15, 2026 at 5:07 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 445k
  • Answers 445k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Certainly the "Single Responsibility Principle" or something near it and… May 15, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer List<string> sqlSortingList = new List<string>(); if (OrderList != null) {… May 15, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer When Com4J generates an event class (ItemEvents in my scenario),… May 15, 2026 at 7:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.