Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21629 > unrolled thread

RE: Style question (Poll)

Started by"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
First post2012-03-14 22:15 +0000
Last post2012-03-14 22:15 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  RE: Style question (Poll) "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-03-14 22:15 +0000

#21629 — RE: Style question (Poll)

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-03-14 22:15 +0000
SubjectRE: Style question (Poll)
Message-ID<mailman.655.1331763352.3037.python-list@python.org>
> >> Which is preferred:
> >>
> >> for value in list:
> >>   if not value is another_value:
> >>     value.do_something()
> >>     break
> 
> Do you really mean 'is' or '=='?

Let me expound on how 'is' and '==' are very different. It may work 
for some comparisons but often not for others. Certain examples work
because of the Python implementation. 

>>> c = 1
>>> d = 1
>>> c is d # This only works because CPython caches small values.
True
>>> c == d
True
>>> a = 10000000000000
>>> b = 10000000000000
>>> a is b
False
>>> a == b
True
>>> 10000000000000 is 10000000000000 
True

'10000000000000 is 10000000000000' works because the interpreter caches
the number because it is on the same line.


Only use 'is' if you are looking for objects like True,
False, None or something that MUST be exactly the same object.

In general, use '=='.



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web