Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'cpython': 0.05; 'subject:Python': 0.06; 'cache': 0.07; 'correct.': 0.07; 'explicit': 0.07; 'none,': 0.07; 'variables': 0.07; '[],': 0.09; 'assigning': 0.09; 'false,': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'python': 0.11; 'question.': 0.14; '"is"': 0.16; '(),': 0.16; '-tkc': 0.16; 'caches': 0.16; 'false:': 0.16; 'ironpython': 0.16; 'normally,': 0.16; 'numbers).': 0.16; 'tim,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'aug': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '>>>': 0.24; 'integer': 0.24; 'received:emailsrvr.com': 0.24; "shouldn't": 0.24; 'versions': 0.24; '>': 0.26; 'compare': 0.26; 'received:(smtp server)': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'on,': 0.29; 'tim': 0.29; 'compared': 0.30; 'code': 0.31; 'chase': 0.31; 'exceptions': 0.31; 'ok.': 0.31; 'values.': 0.31; 'done.': 0.35; 'objects': 0.35; 'but': 0.35; 'there': 0.35; 'false': 0.36; 'i.e.': 0.36; 'doing': 0.36; 'thanks': 0.36; 'possible': 0.36; 'should': 0.36; 'depends': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'future': 0.60; 'catch': 0.60; 'new': 0.61; 'range': 0.61; "you're": 0.61; 'more': 0.64; 'different': 0.65; 'taking': 0.65; 'between': 0.67; 'behavior': 0.77; '257': 0.84; 'location?': 0.84; 'colleagues': 0.97; '2013': 0.98 X-Virus-Scanned: OK Date: Sat, 10 Aug 2013 20:30:17 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130803 Thunderbird/17.0.8 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python Basic Doubt References: <20130810114040.6ac78fe8@bigbox.christie.dr> In-Reply-To: Content-Type: multipart/alternative; boundary="------------050601020000050504040004" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 221 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376191825 news.xs4all.nl 15938 [2001:888:2000:d::a6]:45658 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52350 This is a multi-part message in MIME format. --------------050601020000050504040004 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 08/10/2013 08:09 PM, Krishnan Shankar wrote: > Thanks Tim, > > This takes me to one more question. > > 'is' operator is used to compare objects and it should not be used to > compare data. > > So can it be compared with 'False'. > > i.e. Is this code possible > > if a is False: > print 'Yes' > if b is False: > print 'No' Depends on what you want. If you want to differentiate between a value of False, and other false-like values 0, (), [], {} and so on, then you need to be explicit with if a is False: Normally, that's not what you want, so you use if not a: to catch any of those false-like values. > > Because i recommended this should not be done. But my colleagues say > it is correct. > > Regards, > Krishnan > > > On Sat, Aug 10, 2013 at 10:10 PM, Tim Chase > > > wrote: > > On 2013-08-10 21:03, Krishnan Shankar wrote: > > >>> a=10 > > >>> id(a) > > 21665504 > > >>> b=a > > >>> id(b) > > 21665504 > > >>> c=10 > > >>> id(c) > > 21665504 > > > > I am actually assigning new value to c. But from the value of id() > > all three variables take same location. With variables a and b it > > is ok. But why c taking the same location? > > As an internal optimization, CPython caches small integer values > > >>> a = 256 > >>> b = 256 > >>> a is b > True > >>> a = 257 > >>> b = 257 > >>> a is b > False > > Because it's an internal implementation detail, you shouldn't count > on this behavior (Jython or Cython or IronPython may differ; or > future versions of Python may cache a different range of numbers). > > Generally, if you are using the "is" operator to compare against > anything other than None, you're doing it wrong. There are exceptions > to this, but it takes knowing the particulars. > > -tkc > > > > > > --------------050601020000050504040004 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 08/10/2013 08:09 PM, Krishnan Shankar wrote:
Thanks Tim,

This takes me to one more question. 

'is' operator is used to compare objects and it should not be used to compare data.

So can it be compared with 'False'.

i.e. Is this code possible

if a is False:
    print 'Yes'
if b is False:
    print 'No'

Depends on what you want.  If you want to differentiate between a value of False, and other false-like values 0, (), [], {} and so on, then you need to be explicit with
    if a is False:

Normally, that's not what you want, so you use
    if not a:
to catch any of those false-like values.



Because i recommended this should not be done. But my colleagues say it is correct.

Regards,
Krishnan


On Sat, Aug 10, 2013 at 10:10 PM, Tim Chase <python.list@tim.thechases.com> wrote:
On 2013-08-10 21:03, Krishnan Shankar wrote:
> >>> a=10
> >>> id(a)
> 21665504
> >>> b=a
> >>> id(b)
> 21665504
> >>> c=10
> >>> id(c)
> 21665504
>
> I am actually assigning new value to c. But from the value of id()
> all three variables take same location. With variables a and b it
> is ok. But why c taking the same location?

As an internal optimization, CPython caches small integer values

  >>> a = 256
  >>> b = 256
  >>> a is b
  True
  >>> a = 257
  >>> b = 257
  >>> a is b
  False

Because it's an internal implementation detail, you shouldn't count
on this behavior (Jython or Cython or IronPython may differ; or
future versions of Python may cache a different range of numbers).

Generally, if you are using the "is" operator to compare against
anything other than None, you're doing it wrong. There are exceptions
to this, but it takes knowing the particulars.

-tkc







--------------050601020000050504040004--