473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

String Identity Test

Hi:

I am confused at string identity test:

Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
a="test"
b="test"
a is b True


About identity, I think a is not b, but "a is b" returns True.
Does that mean equality and identity is the same thing for strings?
Nov 1 '05 #1
6 1627
Thomas Moore wrote:
I am confused at string identity test:
<snip>
Does that mean equality and identity is the same thing for strings?

Definitely not. What is actually happening is that certain string literals
get folded together at compile time to refer to the same string constant,
but you should never depend on this happening.

If 'a!=b' then it will also be the case that 'a is not b', but if 'a==b'
then there are no guarantees; any observed behaviour is simply an accident
of the implementation and could change:
a="test 1"
b="test 1"
a is b False a="test"
b="test"
a is b True


Testing for identity is only useful in very rare situations, most of the
time you are better just to forget there is such a test.

Nov 1 '05 #2
Thomas Moore wrote:
a="test"
b="test"
a is b
True

About identity, I think a is not b, but "a is b" returns True.
Does that mean equality and identity is the same thing for strings?


Not exactly:
a="this is also a string"
b="this is also a string"
a is b

False

It's the same with integers. Small ones are shared, big ones aren't.
Details vary with Python version.

Python sometimes optimizes its memory use by reusing immutable objects.
If you've done 'a="test"', and does 'b="test"', Python sees that it can
save some memory here, so instead of creating a new string object on the
heap (which is what happened when you did 'a="test"'), it makes 'b'
refer to that already existing "test" string object that 'a' refers to.
It's roughly as if you would have written 'b=a' instead.

Of course, it would *never* do this for mutable objects.
'a=[];b=[];a.append(1)' must leave b empty, otherwise Python would be
seriously broken. For immutable objects, this isn't a problem though.
Once created, the 'test' string object will always be the same until
it's destroyed by garbage collection etc.

Were you planning to write code that relied on id(x) being different
for different but identical strings x or do you just try to understand
what's going on?
Nov 1 '05 #3
Duncan Booth <du**********@suttoncourtenay.org.uk> wrote:
If 'a!=b' then it will also be the case that 'a is not b'


That's true for strings, and (as far as I know), all pre-defined
types, but it's certainly possible to define a class which violates
that.

class isButNotEqual:
def __ne__ (self, other):
return True

a = isButNotEqual()
b = a
print "a != b:", a != b
print "a is not b:", a is not b
frame:play$ ./eq.py
a != b: True
a is not b: False

On the other hand, I can't imagine any reason why you would want to
define such a class, other than as a demonstration (or part of an
obfuscated Python contest).
Nov 1 '05 #4

"Roy Smith" <ro*@panix.com> wrote in message news:dk**********@panix2.panix.com...
On the other hand, I can't imagine any reason why you would want to
define such a class,


PEP 754?
Nov 1 '05 #5
Hi:
Were you planning to write code that relied on id(x) being different
for different but identical strings x or do you just try to understand
what's going on?

Just try to understand what's going on.....

Thanks All.

Nov 2 '05 #6
"Richard Brodie" <R.******@rl.ac.uk> wrote:

"Roy Smith" <ro*@panix.com> wrote in message news:dk**********@panix2.panix.com...
On the other hand, I can't imagine any reason why you would want to
define such a class,


PEP 754?


My congratulations on a very subtle and somewhat multicultural joke...
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 3 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: grzes | last post by:
MS SQL Server 2000. My case is: I have the table T with primary key calling __recid int without identity property. This table includes a lot of records (about 1000000). I need to convert __recid's...
4
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I...
1
by: Stan | last post by:
I get this error when I try to access any page on the website: An error occurred while try to load the string resources (GetModuleHandle failed with error 126). At the same time here is what I...
3
by: Dan | last post by:
I'm writing a record from an asp.net page to SQL Server. After the insert I'm selecting @@identity to return the ID of the record that I just wrote. It worked fine until I typed a semicolon into...
3
by: asemeiks | last post by:
I'm using Access 97, Jet 4.0. the data resides on a Win 2000 domain server. Using .Net 1.1 and IIS 5.0 on a local XPPro computer I am trying connect to a Jet database on the server. If the data...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
5
by: Veeru71 | last post by:
Given a table with an identity column (GENERATED BY DEFAULT AS IDENTITY), is there any way to get the last generated value by DB2 for the identity column? I can't use identity_val_local() as...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
0
by: Frank Swarbrick | last post by:
So we're trying to decide if it's better to use IDENTITY columns or sequences to create a surrogate key as the primary key for our tables. I kind of like the identity column, because it's more...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.