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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:36:53+00:00 2026-06-14T16:36:53+00:00

I am trying to test a basic premise in python and it always fails

  • 0

I am trying to test a basic premise in python and it always fails and I can’t figure out why.

My sys.argv looks like this:

['test.py', 'test']

And my code looks like this:

if len(sys.argv) > 1 and sys.argv[1] is 'test':
    print 'Test mode'

But the test is never true. I am sure that I am missing something really simple here, but I can’t figure out what it is.

  • 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-14T16:36:54+00:00Added an answer on June 14, 2026 at 4:36 pm

    As mentioned above, the main reason is your test comparison. Using is is different than using == as it compares if two objects are equal. In this case, you can verify that they are not equal by checking their ids:

    import sys
    
    print id(sys.argv[1])
    print id('test')
    

    My output:

    140335994263232
    140335994263424
    

    As they point to different objects, they will not be equal when using is (but using == will compare the strings themselves, which will return True).

    The issue at work here is the concept of interning. When you hardcode two identical strings into your source, the strings are interned and the two will share an object ID (this explains @SamMussmann’s very valid point below). But when you pass a string in via argv, a new object is created, thereby making the comparison to an identical hardcoded string in your code return False. The best explanation I have found so far is in here, where both Alex Martelli and Jon Skeet (two very reputable sources) explain interning and when strings are interned. From these explanations, it does seem that since the data from argv is external to the program, the values aren’t interned, and therefore have different object IDs than if they were both literals in the source.

    One additional point of interest (unrelated to the issue at hand but pertinent to the is discussion) is the caching that is done with numbers. The numbers from -5 to 256 are cached, meaning that is comparisons with equal numbers in that range will be True, regardless of how they are calculated:

    In [1]: 256 is 255 + 1
    Out[1]: True
    
    In [2]: 257 is 256 + 1
    Out[2]: False
    
    In [3]: -5 is -4 - 1
    Out[3]: True
    
    In [4]: -6 is -5 - 1
    Out[4]: False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably very basic but I'm trying to test the Google Places API.
I've been searching all over and can't figure out why this isn't working. I'm
I'm running around in circles trying to figure out why I can't get jQuery
I am trying to test SharePoint's sending of emails - whether basic alerts or
I'm trying to write basic assert test: def assert_session_has ( sessionvar ) return assert_not_nil
I'm trying to write a basic client/server echo program, to test the usage of
just trying to test for equality in this piece of code, but getting a
I'm trying to test some mailers with rspec but deliveries are always empty. Here
I'm trying to write a basic test game state singleton in cocos2d, but for
I'm trying to test out Spring Annotations to see how they work with some

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.