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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:23:33+00:00 2026-06-13T12:23:33+00:00

This is just a curiosity question for a good pythonic way to do this.

  • 0

This is just a curiosity question for a “good” pythonic way to do this.

I have a function with an optional parameter ie.

def foo( a, b, c='en' ):
    print c

There is a dict with a bunch of info in it, and if a particular key is in the dict, I would like to pass it into foo to override c’s default, but if the key is not in the dict, I just want to use the default for c.

Obviously this will work…

if "SomeKey" in mydict:
    foo( val1, val2, mydict[ "SomeKey" ]
else:
    foo( val1, val2 )

And another option would be to do something like

params = [ val1, val2 ]
if "SomeKey" in mydict:
    params.append( mydict[ "SomeKey" ] )
foo( *params )

but there must be a slick, more pythonic way to do this? ie.

foo( val1, val2, mydict[ "SomeKey" ] if "SomeKey" in mydict else < use default > )

Thanks in advance!

  • 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-13T12:23:33+00:00Added an answer on June 13, 2026 at 12:23 pm

    Don’t use a “true” default value for this – instead, use a placeholder value:

    def foo( a, b, c=None ):
        if c is None:
            c = 'en'
        print c
    

    and then just call with…

    foo( val1, val2, mydict.get("SomeKey") )
    

    (.get() returns None if the key isn’t in the dict, by default)


    If you can’t modify foo(), then you can do the more complex varargs path:

    maybe_c = {'c': mydict["SomeKey"]} if "SomeKey" in mydict else {}
    foo( val1, val2, **maybe_c )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a stupid question. But just out of curiosity, is there
This is just an out of curiosity question. Let's say you have a database
Just a curiosity question! This seems to suggest that member function pointers are in
This is just a curiosity question I was wondering if anyone had a good
This is just a curiosity - I don't have a real question. The output
This is just a question out of curiosity since I have been needing to
This is just a curiosity question. I know that Spring can be used in
Just a general curiosity question. Why is this namespace System.Collections.Generic added by default in
This is mostly just out of curiosity, and is potentially a silly question. :)
This question is just out of curiosity. In recursive templates if we forget to

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.