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 8154941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:31:08+00:00 2026-06-06T16:31:08+00:00

I have a function with two optional parameters: def func(a=0, b=10): return a+b Somewhere

  • 0

I have a function with two optional parameters:

def func(a=0, b=10):
    return a+b

Somewhere else in my code I am doing some conditional argument passing like:

if a and b:
   return func(a, b)
elif a:
   return func(a)
elif b:
   return func(b=b)
else:
   return func()

Is there anyway to simplify code in this pattern?

EDIT:

Let’s assume that I’m not allowed to implement default parameter logic inside func.

I may have several functions like func: func1, func2 and func3 would all contain the

a = a or 0
b = b or 10

statements.

But I’m invoking these series of functions to eliminate duplication. (using a decorator)

  • 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-06-06T16:31:10+00:00Added an answer on June 6, 2026 at 4:31 pm

    If you don’t want to change anything in func then the sensible option would be passing a dict of arguments to the function:

    >>> def func(a=0,b=10):
    ...  return a+b
    ...
    >>> args = {'a':15,'b':15}
    >>> func(**args)
    30
    >>> args={'a':15}
    >>> func(**args)
    25
    >>> args={'b':6}
    >>> func(**args)
    6
    >>> args = {}
    >>> func(**args)
    10
    

    or just:

    >>>func(**{'a':7})
    17
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two function template overloads: template<typename T> optional<T> some(const T& x)
I have a function which takes in two parameters, and returns one or the
I have two functions: def f(a,b,c=g(b)): blabla def g(n): blabla c is an optional
Is it possible to have two function calls in a onclick? I tried doing
I have a JavaScript function that takes two required parameters and then arbitrarily many
Can Go have optional parameters? Or can I just define two different functions with
i have function that takes two parameters of type LPTSTR, and i am trying
I have two function and I need to execut in one onClick . The
I have a function with two input variables min.depth<-2 max.depth<-5 the function produces a
I have a function that returns two values in a list. Both values need

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.