Thanks to all for your response.
Le 27/11/2011 00:01, Steven D'Aprano a écrit :
On Sat, 26 Nov 2011 22:20:36 +0100, candide wrote:
In which cases should we use the is() function ? The is() function
compares identity of objects rather than values so I was wondering in
which circumstances comparing identities of objects is really vital.
`is` is not a function. It is a keyword and an operator.
oops exponent 10 !! I have in mind the id() function, very close to the
is operator. An operator named "is" makes search of code snippets very
complicated because the verb "is" is always embedded in comments or
documentation.
But how much "spam is None" is different from "spam == None" ?
Even if you can guarantee that your code base does not contain any object
which compares equal to None except for None itself (and how would you do
that? a full audit of every line of code in every library you use?), the
use of `is` should be preferred because it signals your intention much
better.
OK but tons of good code use "spam == None" ; for instance, many tests
files in Python official code. A random example (from
openshot/openshot/windows/MainGTK.py):
# ---------------------------------------------------------------
parent_name = item.parent
if parent_name == None:
match_iter = None
# Check for NO files
if mode == None and self.project.project_folder.items.__len__()
== 0:
#switch to the detail view
if drop_track == None:
# keep old parent, if no track found
if self.new_clip_object == None:
self.item_detected = False
# ---------------------------------------------------------------
If your intention is to accept arbitrary objects which compare equal to
None, than by all means use == for your comparison. But normally the
intention is to accept None, and nothing else.
So, for the same reason, wouldn't it be better to use "if spam is True"
against to "if spam == True" (or better "if spam") ?
That is correct. You probably should rarely use `is`. Apart from testing
for None, use of `is` should be rare.
OK, thanks
--
http://mail.python.org/mailman/listinfo/python-list