From: saeed.gnu on 9 Aug 2010 07:41 "x is y" means "id(y) == id(y)" "x is not y" means "id(x) != id(x)" "x is not None" means "id(x) != id(None)" "x is not None" is a really silly statement!! because id(None) and id of any constant object is not predictable! I don't know whay people use "is" instead of "==". you should write "if x!=None" instead of "x is not None"
From: saeed.gnu on 9 Aug 2010 08:11 On Aug 9, 3:41 pm, "saeed.gnu" <saeed....(a)gmail.com> wrote: > "x is y" means "id(y) == id(y)" > "x is not y" means "id(x) != id(x)" > "x is not None" means "id(x) != id(None)" > > "x is not None" is a really silly statement!! because id(None) and id > of any constant object is not predictable! I don't know whay people > use "is" instead of "==". you should write "if x!=None" instead of "x > is not None" Although small objects are unique in the memory (with a unique id) and using "is" works ok, but that's not logical to compare id's when we actually want to compare values!
From: Dave Angel on 9 Aug 2010 10:21 saeed.gnu wrote: > On Aug 9, 3:41 pm, "saeed.gnu" <saeed....(a)gmail.com> wrote: > >> "x is y" means "id(y) =id(y)" >> "x is not y" means "id(x) !=d(x)" >> "x is not None" means "id(x) !=d(None)" >> >> "x is not None" is a really silly statement!! because id(None) and id >> of any constant object is not predictable! I don't know whay people >> use "is" instead of "=. you should write "if x!=None" instead of "x >> is not None" >> > > Although small objects are unique in the memory (with a unique id) and > using "is" works ok, but that's not logical to compare id's when we > actually want to compare values! > > None is a single object; there is only one such object by language definition. So 'is' is the perfect operator if you want to check whether a particular object is that one or not. All other objects, of any possible type, will have a different ID than None. And talking about comparing values is nonsense anyway, since None has no value. It's only purpose is to indicate that some operation had no value, or no return statement, or ... Using '==' will not necessarily give the same result, and I can't think of ANY case where I'd prefer the former. If you disagree, give a concrete example. DaveA DaveA
From: Nobody on 9 Aug 2010 10:56 On Mon, 09 Aug 2010 04:41:23 -0700, saeed.gnu wrote: > "x is not None" is a really silly statement!! because id(None) and id > of any constant object is not predictable! I don't know whay people > use "is" instead of "==". you should write "if x!=None" instead of "x > is not None" No, you should use the identity check. If you use ==, the operand may have an __eq__ or __cmp__ method which considers the object equal to None.
From: Terry Reedy on 9 Aug 2010 12:28
On 8/9/2010 7:41 AM, saeed.gnu wrote: > "x is y" means "id(y) == id(y)" > "x is not y" means "id(x) != id(x)" > "x is not None" means "id(x) != id(None)" > > "x is not None" is a really silly statement!! Wrong. It is exactly right when that is what one means and is the STANDARD IDIOM. > because id(None) and id > of any constant object is not predictable! This is silly. The id of None and of any other object are predictably different. > I don't know whay people use "is" instead of "==". Because it is the right thing to do! > you should write "if x!=None" instead of "x is not None" Wrong. It is trivial to make a class whose objects compare == to None. -- Terry Jan Reedy |