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


Groups > comp.lang.python > #52350

Re: Python Basic Doubt

Date 2013-08-10 20:30 -0700
From Gary Herron <gary.herron@islandtraining.com>
Subject Re: Python Basic Doubt
References <CAL0E0u6wO_UBniWoSpePvhKhPDG_nf4p1rqYYrGwzoHTqp6ZHA@mail.gmail.com> <20130810114040.6ac78fe8@bigbox.christie.dr> <CAL0E0u69NXDrtojK4dUY+EcP5e9ab5BYkXb-M3Dz=+BTWpKmSA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.457.1376191825.1251.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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 <mailto: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
>
>
>
>
>
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python Basic Doubt Gary Herron <gary.herron@islandtraining.com> - 2013-08-10 20:30 -0700

csiph-web