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

  • SEARCH
  • Home
  • 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 6225545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:51:43+00:00 2026-05-24T08:51:43+00:00

I have the following function: def foo(**kwargs): if not kwargs: # No keyword arguments?

  • 0

I have the following function:

def foo(**kwargs):
  if not kwargs:
    # No keyword arguments? It's all right. Set defaults here...
  elif ('start_index' or 'end_index') in kwargs:
    # Do something here...
  else:
    # Catch unexpected keyword arguments
    raise TypeError("%r are invalid keyword arguments" % (kwargs.keys())

Question:

I want to make sure that the only valid keyword arguments are start_index or end_index. Anything else will raise an error, even if mixed with the valid ones. What’s the cookbook recipe to make sure that only start_index or end_index are accepted? Yes, I’m looking for a cookbook recipe but I’m not sure how to search for it. I’m not sure if using an if-elif-else structure is the correct way to do it either.

  • 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-24T08:51:45+00:00Added an answer on May 24, 2026 at 8:51 am

    For the sake of completeness, Here’s an alternative that still uses **kwargs.

    def foo(**kwargs):
      start_index = kwargs.pop('start_index', STARTINDEX_DEFAULT)
      end_index = kwargs.pop('end_index', ENDINDEX_DEFAULT)
      if kwargs:
        # Catch unexpected keyword arguments
        raise TypeError("%r are invalid keyword arguments" % (kwargs.keys())
      # Do something here...
    

    But, you shouldn’t want to use this when you don’t absolutely need it, use regular parameters with default values (as in Roman Bodnarchuk’s answer).

    Cases when you might need this is when you also want to use *args, and need a way to distinguish the keyword arguments from arbitrarily man positional arguments. using **kwargs this way forces the keyword arguments to be passed as keywords; A positional argument can never find its way into **kwargs.

    Another reason is so that you can really distinguish between a default and an explicit parameter which happens to be the default. None is often used as a default value for arguments to indicate “the argument doesn’t apply”, but sometimes you actually need to interpret the None as something other than a default. Checking for the presence or absence of a key in the **kwargs dict can accurately distinguish between these cases. (An alternative is to create an instance of a subclass of object whos sole purpose is to be the default value of a specific argument to that specific function)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have following code: def foo(s): A dummy function foo. For example: >>>
If I have a function like the following: def foo(): return 1, 2 I
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
I have the following function: >>> def rule(x): ... rule = bin(x)[2:].zfill(8) ... rule
In Scala (2.7), if I have this function: def foo(args: Array[String]) = for (arg
I have the following code (minus some other operations): def foobar(): msg=None if foo:
Is filter/map equivalent to list comprehension? Suppose I have the following function def fib_gen():
I have written the following class: class myClass(object): def __init__(self): pass def foo(self, arg1,
I have the following function: def heading_positions(self): return map( lambda h: {'{t}.{c}'.format(t=h.table_name,c=h.column_name) : h.position
I have the following function: function calculateAspect(options){ def = { orgw: 0, orgh: 0,

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.