Subscribe to MentalFloss - PyBlog        RSS Feed
-----

Interesting Behavior

Icon 1 Comments
>>> id('4271')
140160694513904
>>> secret = '4271'
>>> id(secret)
140160694514048
>>> id('4271')
140160694514048
>>> guess = raw_input("Guess: ")
Guess: 4271
>>> id(guess)
140160694513952
>>> id('4271')
140160694514048
>>> guess
'4271'
>>> guess is '4271'
False
>>> guess == '4271'
True
>>> name = 'mentalfloss'
>>> name is 'mentalfloss'
True



Not quite sure what that's about yet.




It turns out that this is in regards to the way strings are handled internally. This link explains it a bit better than I can: http://stackoverflow...tring-interning

Oh and using is for string compare is bad practice.

1 Comments On This Entry

Page 1 of 1

andrewsw Icon

28 March 2014 - 05:35 PM

Quote

Oh and using is for string compare is bad practice.

Yes, string interning is implementation specific and should not be relied upon, which is why the is operator shouldn't be used on strings.
1
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

November 2014

S M T W T F S
      1
2345678
9101112131415
16171819202122
2324 25 26272829
30      

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    1 user(s) viewing

    1 Guests
    0 member(s)
    0 anonymous member(s)