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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:09:09+00:00 2026-05-11T13:09:09+00:00

Is it bad practice to use the following format when my_var can be None?

  • 0

Is it bad practice to use the following format when my_var can be None?

if my_var and 'something' in my_var:     #do something 

The issue is that 'something' in my_var will throw a TypeError if my_var is None.

Or should I use:

if my_var:     if 'something' in my_var:         #do something 

or

try:     if 'something' in my_var:         #do something except TypeError:     pass 

To rephrase the question, which of the above is the best practice in Python (if any)?

Alternatives are welcome!

  • 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. 2026-05-11T13:09:10+00:00Added an answer on May 11, 2026 at 1:09 pm

    It’s safe to depend on the order of conditionals (Python reference here), specifically because of the problem you point out – it’s very useful to be able to short-circuit evaluation that could cause problems in a string of conditionals.

    This sort of code pops up in most languages:

    IF exists(variable) AND variable.doSomething()     THEN ... 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it bad practice to use the instanceof operator in the following context? public
Consider the following test case, is it a bad practice to use the hashCode
1) Isn't it a bad practice to use these attributes on all of my
I keep hearing that catch (Exception ex) Is bad practise, however, I often use
Can I use the following across all browsers? <a href=# onclick=doSomething()>Click here.</a> Is this
I was wondering if the following code shows a bad practice (about interface inheritance):
I know that having diamond inheritance is considered bad practice. However, I have 2
I know it's a bad practice to use database in the view. However, I'm
Why is it bad practice to declare variables on one line? e.g. private String
Is type checking considered bad practice even if you are checking against an interface?

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.