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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:05:13+00:00 2026-05-17T20:05:13+00:00

HI ! I guess everything is in the question … I was just wondering

  • 0

HI !

I guess everything is in the question …
I was just wondering if there is a nice way in Python to shorten this pattern :

something = get_something()
if something:
    do_a_thing_with(something)

Meaning that I would like to enter in the if context only if the variable something is not None (or False), and then in this context having this variable set automatically ! Is it possible with with statement ?

PS : I don’t want to have to DEFINE more stuff… I am looking for some statement to use on the fly ?!

  • 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-17T20:05:14+00:00Added an answer on May 17, 2026 at 8:05 pm

    This is as pythonic as it gets.

    Things should be no more simplified than they are and no more complex than they should be.

    See how the with statement works and providing a context guard. would be complicated enough.

    • http://effbot.org/pyref/with.htm
    • http://effbot.org/pyref/context-managers.htm
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I guess this is a simple question. I need to do something like this:
Ok, I guess this is a pretty easy solution question. Everything that is shown
I guess my general question which I googled to no luck: Is there a
I guess my main question is, will this always work as long as I
This is a question about best practices i guess but it applies directly to
I guess this is kind of a programming question, because I'm going to write
I guess this question isn't only specific to YUI, but that's the JS library
I have already looked at this question but there was no solution. Here is
Simple question (I guess). I have this default google tutorial code for tabs: intent
I guess this is a question straight to Mark, but I can't for the

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.