I am trying to compare two lines with . One line is returned by the function, and the other is simply specified by comparison. - these are tests for identifying an object, but according to this page , it also works with two identical strings due to Python memory optimization. But the following does not work:
def uSplit(ustring): #return user minus host return ustring.split('!',1)[0] user = uSplit('theuser!host') print type(user) print user if user is 'theuser': print 'ok' else: print 'failed' user = 'theuser' if user is 'theuser': print 'ok'
Exit:
type 'str'
theuser
failed
ok
I assume the reason is that the string returned by the function is a different "type" of the string than the string literal. Is there a way to get a function to return a string literal? I know I can use == , but I'm just curious.
python string-literals string-comparison
pique oil Aug 01 '09 at 9:44 2009-08-01 09:44
source share