Re: [Tutor] is gotchas?
Hugo González Monteverde wrote: >> Hmmm! Hmmm! >> Lookee here: >> ## console session >> >>> a=[1,2] >> >>> b=[1,2] >> >>> a is b >> False >> >>> c='1' ## one byte >> >>> d='1' ## one byte >> >>> c is d >> True >> >>> c='1,2' >> >>> d='1,2' >> >>> c is d >> False >> >> The Hmmm! is emmitted because I'm thinking that if everything is an >> object in python, then why does `c is d` evaluate to True when >> the assigned value is 1 byte and evaluate to False when the assigned >> value is more that 1 byte? > > One and two byte strings are currently optimized in cPython as the same > object, referenced multiple times. It's not just one-byte strings; I think any string that can be a Python identifier is interned and will always be the same string. In [1]: a='abcd' In [2]: b='abcd' In [3]: a is b Out[3]: True > > Note that this is not to be relied upon! Jython, Ironpython, Python3000 > or cPython itself may break it! Right. But why do you even care? I don't think I have ever written code that compares strings using 'is'; just use ==. > > Still cannot find a reference doc for this Probably the source code will be the only reference, this is an implementation detail. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is gotchas?
> Hmmm! Hmmm! > Lookee here: > ## console session > >>> a=[1,2] > >>> b=[1,2] > >>> a is b > False > >>> c='1' ## one byte > >>> d='1' ## one byte > >>> c is d > True > >>> c='1,2' > >>> d='1,2' > >>> c is d > False > > The Hmmm! is emmitted because I'm thinking that if everything is an > object in python, then why does `c is d` evaluate to True when > the assigned value is 1 byte and evaluate to False when the assigned > value is more that 1 byte? One and two byte strings are currently optimized in cPython as the same object, referenced multiple times. Note that this is not to be relied upon! Jython, Ironpython, Python3000 or cPython itself may break it! Still cannot find a reference doc for this Hugo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is gotchas?
> > a is b <==> id(a) == id(b) > > No, not necessarily. id's are recycled which can lead to unexpected > behaviour when comparing them. See for example this thread on c.l.py: > http://tinyurl.com/yflknx wow, that is totally mind-blowing.. good post. i think that for most normal objects that are *not* created on the fly, the comparison is ok. still, i'd stick to using is... as-is. -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is gotchas?
wesley chun wrote: >> 3)is there a special method for `is'. > > no, not really. you can use id() and '==' to proxy for is: > > a is b <==> id(a) == id(b) No, not necessarily. id's are recycled which can lead to unexpected behaviour when comparing them. See for example this thread on c.l.py: http://tinyurl.com/yflknx Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is gotchas?
others already gave good responses, so mine are short. > 1)where do `is` and `==` yield the same results? "is" is object identity comparison while "==" is object value comparison > 2)where do they not yield the same results? they give different results when you have two different objects regardless of the equality of their values. > 3)is there a special method for `is'. no, not really. you can use id() and '==' to proxy for is: a is b <==> id(a) == id(b) > 4)Pointers to docs are welcome. http://docs.python.org/lib/comparisons.html http://docs.python.org/ref/comparisons.html hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is gotchas?
On 11/6/06, Tim Johnson <[EMAIL PROTECTED]> wrote: * Kent Johnson <[EMAIL PROTECTED]> [061106 10:31]:>> In [9]: a=[1,2]>> In [10]: b=[1,2] Hmmm! Hmmm! Lookee here: ## console session >>> a=[1,2]>>> b=[1,2]>>> a is bFalse>>> c='1' ## one byte>>> d='1' ## one byte>>> c is dTrue>>> c='1,2'>>> d='1,2' >>> c is dFalseThe Hmmm! is emmitted because I'm thinking that if everything is anobject in python, then why does `c is d` evaluate to True whenthe assigned value is 1 byte and evaluate to False when the assigned value is more that 1 byte?I think I ran into this before and that's why I never used `is'.You might want to try:>>> a = 'a'>>> b = 'a'>>> a is b TrueWhy? Interned strings. As Pujo aluded to, various simple or well-used objects (like the digits between 0 and 100), and strings that have been interned with the intern() function.There is a unique item: None. There is only one object of type NoneType. No matter how many times you reference it, it will always be the same object. So it is a perfect use of "is": def f(arg1, arg2, optarg=None): if optarg is None: -Arcege-- There's so many different worlds,So many different suns.And we have just one world,But we live in different ones. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is gotchas?
c='1' ## one byte d='1' ## one byte c is d > True c='1,2' d='1,2' c is d > False > > The Hmmm! is emmitted because I'm thinking that if everything is an > object in python, then why does `c is d` evaluate to True when > the assigned value is 1 byte and evaluate to False when the assigned > value is more that 1 byte? 'is' is the operator you want if you want to check for object identity. You should probably not use it for strings, or for immutables for that matter. Its value here is "undefined" above in the sense that our programs shouldn't depend on the behavior you're seeing there. (And you'll see different behavior depending on if Python is being run as an interpreter vs. on a whole program. On a whole program, all the duplicate string literals tend to be shared since Python strings are immutable.) A closer look at: http://docs.python.org/ref/objects.html is helpful, especially near the bottom. 'is' is relatively rare; I've seen it used most often in code dealing with object caches. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is gotchas?
For small string object python has optimization method so it is really the same object!Cheers,pujoOn 11/7/06, Tim Johnson < [EMAIL PROTECTED]> wrote:* Kent Johnson < [EMAIL PROTECTED]> [061106 10:31]:>> In [9]: a=[1,2]>> In [10]: b=[1,2] Hmmm! Hmmm! Lookee here: ## console session>>> a=[1,2]>>> b=[1,2]>>> a is b False>>> c='1' ## one byte>>> d='1' ## one byte>>> c is dTrue>>> c='1,2'>>> d='1,2'>>> c is dFalseThe Hmmm! is emmitted because I'm thinking that if everything is an object in python, then why does `c is d` evaluate to True whenthe assigned value is 1 byte and evaluate to False when the assignedvalue is more that 1 byte?I think I ran into this before and that's why I never used `is'. Good thread. Beats flame wars.thankstim--Tim Johnson <[EMAIL PROTECTED]> http://www.alaska-internet-solutions.com ___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is gotchas?
* Kent Johnson <[EMAIL PROTECTED]> [061106 10:31]: > > In [9]: a=[1,2] > > In [10]: b=[1,2] Hmmm! Hmmm! Lookee here: ## console session >>> a=[1,2] >>> b=[1,2] >>> a is b False >>> c='1' ## one byte >>> d='1' ## one byte >>> c is d True >>> c='1,2' >>> d='1,2' >>> c is d False The Hmmm! is emmitted because I'm thinking that if everything is an object in python, then why does `c is d` evaluate to True when the assigned value is 1 byte and evaluate to False when the assigned value is more that 1 byte? I think I ran into this before and that's why I never used `is'. Good thread. Beats flame wars. thanks tim -- Tim Johnson <[EMAIL PROTECTED]> http://www.alaska-internet-solutions.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is gotchas?
Tim Johnson wrote: > I've been using python is 1.5* but haven't used `is` that much. > > 1)where do `is` and `==` yield the same results? > > 2)where do they not yield the same results? is tests for object identity - 'a is b' is true if a is the same object as b. There is no special method, no specialization for different data types, just 'is it the same thing?' == tests for equality of value. It's exact meaning depends on the types of objects your are comparing. It can be specialized by defining a special method. a is b generally implies a == b because if a is b then a==b is the same as a==a which is true for pretty much anything - it would be a very strange type for which a == a is not true. Maybe NaN (Not a Number) compares false to itself, I'm not sure, I don't know how to create NaN on Windows. But really the meaning of == is defined by the underlying type so in your own classes you can make it do anything you want. a==b does not imply a is b - it is quite reasonable for two different things to have the same value: In [9]: a=[1,2] In [10]: b=[1,2] In [11]: a is b Out[11]: False In [12]: a==b Out[12]: True In [13]: a='abc**def' In [14]: b='abc' + '**def' In [15]: a is b Out[15]: False In [16]: a==b Out[16]: True > > 3)is there a special method for `is'. No. Kent > > 4)Pointers to docs are welcome. > > thanks > tim > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor