Quote aware split

obaudys at gmail.com obaudys at gmail.com
Wed May 16 01:44:51 EDT 2007


On May 16, 1:18 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:

> The "is" operator checks object *identity*, that is, if both operands are
> actually the same object. It may or may not work here, depending on many
> implementation details. You really should check if they are *equal*
> instead:
>
>           if c == quote:
>               qcount += 1
>           if c == sep and qcount % 2 == 0:
>               splitpoints.append(index)

I was always under the impression the 'a' and 'a' were always the same
object and
*is* and == were interchangeble with strings, since they were
immutable.  But
sure enough running your code snippet example:

> See:
> py> x='a'
> py> y='A'.lower()
> py> y
> 'a'
> py> x==y
> True
> py> x is y
> False

I got the same result as you.  Point taken, thank you!

Interestingly:

py> id('a')
-1209815264
py> id(x)
-1209815264
py> id(y)
-1210219808
py> id('A'.lower())
-1210219712

So there are at least three 'a' string objects registered in whatever
hashtable implementation
lies underneath Python's string datatype.





More information about the Python-list mailing list