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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:38:01+00:00 2026-05-19T17:38:01+00:00

I was wondering if there was a way to do something like this in

  • 0

I was wondering if there was a way to do something like this in Python:

some_method(param1="param1", param2 = somevar or None)

I want to pass the param2 if somevar is set, or None if it isn’t. Basically I am looking for a shorter way of doing this:

if somevar is None:
some_method(param1="param1")
else:
some_method(param1="param1", param2=somevar)
  • 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-19T17:38:02+00:00Added an answer on May 19, 2026 at 5:38 pm

    Keyword arguments default to what you provide them in your function definition.

    def some_method(param1="param1", param2=None):
        # param1 is "param1" unless some other value is passed in.
        # param2 is None if some value is not passed for it.
    

    Consider:

    def print_values(first_value="Hello", second_value=None):
        print first_value, second_value or "World!"
    
    print_values() #prints Hello World!
    print_values(first_value="Greetings") #prints Greetings World!
    print_values(second_value="You!") # prints Hello You!
    print_values(first_value="Greetings", second_value="You!")
    # prints Greetings You!
    

    If you want to print the value of a local variable, you can do so by adding the expected variable name to your definition:

    print first_value, special_variable_name or second_value or "World!"
    

    Then, if you define a variable with that name:

    special_variable_name = "Sean"
    print_values() # prints Hello Sean
    

    But don’t do this — it is unexpected, magic and definitely un-Pythonic — pass the value you need in as your second parameter instead.

    If you need to be able to call it with that variable without errors, then make sure to define that variable at the top of your module.

    special_variable_name = None
    # ... much code later ...
    special_variable_name = "Sean" #This can be commented out
    print_values(second_value=special_variable_name) # prints Hello Sean
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if something like this is possible in python (3.2, if that's relevant).
Just wondering is there any way I can check whether the url links to
I was wondering is there a way to give the user a small guided
I was wondering is there a way in visual studio 2010 to search in
I was wondering is there a way in PHP that you could tell where
I am wondering is there a way to render a partial view in the
I am wondering is there any way we can test the font size/color of
I am wondering is there any way to execute following shell script, which waits
Wondering if there is any way to get the lambda expressions that result from
just wondering if there is a way to reduce the amount of code needed

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.