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


Groups > comp.lang.python > #16323

Re: Pragmatics of the standard is() function

Date 2011-11-28 13:57 +0100
From Jean-Michel Pichavant <jeanmichel@sequans.com>
Subject Re: Pragmatics of the standard is() function
References <4ed15825$0$21841$426a34cc@news.free.fr>
Newsgroups comp.lang.python
Message-ID <mailman.3094.1322485045.27778.python-list@python.org> (permalink)

Show all headers | View raw


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.
>
> Examining well reputated Python source code, I realize that is() 
> function is mainly used in the following set form :
>
> spam is None
>
> But how much "spam is None" is different from "spam == None" ?
>
>
>
> is() function makes comparaison of (abstract representation of) 
> adresses of objects in memory. Comparing addresses of objects is a low 
> level feature performed by low level langages such as C but seldom 
> needed in high level languages like Python, isn'it ?
I remember meeting a use case where testing identity is required, when 
you are searching for an instance containing a specific object:

class Someone:
    def __init__(self, name, car):
        self.name = name
        self.car = car

class Car:
    def __init__(self, brand):
        self.brand = brand
    def __eq__(self, other):
        return self.brand == other.brand

people = { 'bob':Someone('bob', Car('chrys')), 'cindy': Someone('cindy', 
Car('Volk')), 'carlos':Someone('carlos', Car('Volk'))}
aCar = people['carlos'].car
print "people owning a Volk car", [ people[ppl].name for ppl in people 
if people[ppl].car == Car('Volk')]
print "people owning Carlos's car", [ people[ppl].name for ppl in people 
if people[ppl].car is aCar]

people owning a Volk car ['carlos', 'cindy']
people owning Carlos's car ['carlos']

JM

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


Thread

Pragmatics of the standard is() function candide <candide@free.invalid> - 2011-11-26 22:20 +0100
  Re: Pragmatics of the standard is() function Roy Smith <roy@panix.com> - 2011-11-26 16:32 -0500
  Re: Pragmatics of the standard is() function Chris Angelico <rosuav@gmail.com> - 2011-11-27 09:22 +1100
  Re: Pragmatics of the standard is() function Alexander Kapps <alex.kapps@web.de> - 2011-11-26 23:38 +0100
  Re: Pragmatics of the standard is() function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-26 23:01 +0000
    Re: Pragmatics of the is operator candide <candide@free.invalid> - 2011-11-27 02:42 +0100
      Re: Pragmatics of the is operator Chris Angelico <rosuav@gmail.com> - 2011-11-27 12:50 +1100
      Re: Pragmatics of the is operator Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-27 03:13 +0000
    Re: Pragmatics of the standard is() function Den <patentsvnc@gmail.com> - 2011-11-28 11:22 -0800
      Re: Pragmatics of the standard is() function Ethan Furman <ethan@stoneleaf.us> - 2011-11-28 12:05 -0800
      Re: Pragmatics of the standard is() function alex23 <wuwei23@gmail.com> - 2011-11-28 19:58 -0800
      Re: Pragmatics of the standard is() function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-29 08:41 +0000
        Re: Pragmatics of the standard is() function Den <patentsvnc@gmail.com> - 2011-11-29 09:11 -0800
  Re: Pragmatics of the standard is() function Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-11-28 13:57 +0100

csiph-web