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

October 2014

S M T W T F S
   1234
567891011
12131415161718
192021222324 25
262728293031 

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

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