not and is not problem

J dreadpiratejeff at gmail.com
Mon Jan 18 11:11:35 EST 2010


On Mon, Jan 18, 2010 at 10:43, superpollo <utente at esempio.net> wrote:
> hi:
>
> #!/usr/bin/env python
> data = "seq=123"
> name , value = data.split("=")
> print name
> print value
> if not name == "seq":
>    print "DOES NOT PRINT OF COURSE..."
> if name is not "seq":
>    print "WTF! WHY DOES IT PRINT?"

is will return True if two variables point to the same object, == if
the objects referred to by the variables are equal.

>> if not name == "seq":

says if the object that the variable name points to is NOT the same as
the string "seq" then do the following.
Since the object that "name" points to contains the string "seq" and
the string "seq" is identical in value to the "seq" in your
comparison, the result is TRUE (b = a) and your if statment only
proceeds if the comparison result is FALSE.

>> if name is not "seq"

is and is not relate to pointing to an object, not the object's contents.

Example:

>> name = "foo"
>> name1 = name
>> print name
foo
>> print name1
foo
>> name is name1
True
>> name1 is name
True
>> name = "bar"
>> print name
bar
>> print name1
foo
>> name is name1
False
>> name is not name1
True

Or even better:

>> name = "foo"
>> name1 = name
>> id(name)
11875840
>> id(name1)
11875840
>> name = "bar"
>> id(name)
11875744
>> id(name1)
11875840
>> id("foo")
11875840
>> id("bar")
11875744

shows the location for each object in relation to the name pointing to
that object...

-- 

Joan Crawford  - "I, Joan Crawford, I believe in the dollar.
Everything I earn, I spend." -
http://www.brainyquote.com/quotes/authors/j/joan_crawford.html



More information about the Python-list mailing list