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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:13:21+00:00 2026-05-23T08:13:21+00:00

Does a Python equivalent to the Ruby ||= operator (set the variable if the

  • 0

Does a Python equivalent to the Ruby ||= operator (“set the variable if the variable is not set”) exist?

Example in Ruby :

 variable_not_set ||= 'bla bla'
 variable_not_set == 'bla bla'

 variable_set = 'pi pi'
 variable_set ||= 'bla bla'
 variable_set == 'pi pi'
  • 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-23T08:13:22+00:00Added an answer on May 23, 2026 at 8:13 am

    No, the replacement is:

    try:
       v
    except NameError:
       v = 'bla bla'
    

    However, wanting to use this construct is a sign of overly complicated code flow. Usually, you’d do the following:

    try:
       v = complicated()
    except ComplicatedError: # complicated failed
       v = 'fallback value'
    

    and never be unsure whether v is set or not. If it’s one of many options that can either be set or not, use a dictionary and its get method which allows a default value.

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

Sidebar

Related Questions

Possible Duplicate: Python Ternary Operator Does Python have an equivalent of the ternary operator?:
Does anyone know of a Python equivalent for FMPP the text file preprocessor? Follow
Does python have the ability to create dynamic keywords? For example: qset.filter(min_price__usd__range=(min_price, max_price)) I
Does BeautifulSoup work with Python 3? If not, how soon will there be a
What is the equivalent of the backticks found in Ruby and Perl in Python?
Does python have an equivalent to Tcl's uplevel command? For those who don't know,
Does Python or any of its modules have an equivalent of MATLAB's conv2 function?
Is there an equivalent of slime for python? For example, if I position the
When Python is running under Windows, time.localtime does not report the correct time if
What is the Lua equivalent of Twisted Python, Eventmachine for Ruby, NIO for Java,

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.