473,320 Members | 2,117 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,320 software developers and data experts.

string slicing

Hello all ,
I am trying some interactive examples here where i have come across
inconsistencies??? :O
Anyway heres whats bothering me
s = 'hello'
s[0] 'h' s[:] 'hello' m = s[:]
m 'hello' m is s

True

I discussed the *is* operator with some of the pythoners before as
well but it is somewhat different than what i intended it to do. The
LP2E by Mark & David says -
" m gets a *full top-level copy* of a sequence object- an object with
the same value but distinct piece of memory." but when i test them
with *is* operator then the result is True. Why is this happening??
Any help is appreciated.. Thanx
--
cheers,
Ishwor Gurung
Jul 18 '05 #1
3 1318
Ishwor wrote:
s = 'hello'
m = s[:]
m is s
True

I discussed the *is* operator with some of the pythoners before as
well but it is somewhat different than what i intended it to do. The
LP2E by Mark & David says -
" m gets a *full top-level copy* of a sequence object- an object with
the same value but distinct piece of memory." but when i test them
with *is* operator then the result is True. Why is this happening??


This behaviour is due to the way strings are handled. In some cases strings are 'interned' which
lets the interpreter keep only a single copy of a string. If you try it with a list you get a
different result:
s=list('hello')
s ['h', 'e', 'l', 'l', 'o'] m=s[:]
m ['h', 'e', 'l', 'l', 'o'] m is s

False

Kent
Jul 18 '05 #2
On Sun, 05 Dec 2004 09:44:13 -0500, Kent Johnson <ke******@yahoo.com> wrote:

[snip]

This behaviour is due to the way strings are handled. In some cases strings are 'interned' which
lets the interpreter keep only a single copy of a string. If you try it with a list you get a
different result:
s=list('hello')
s ['h', 'e', 'l', 'l', 'o'] m=s[:]
m ['h', 'e', 'l', 'l', 'o'] m is s False

Kent


Thanx Kent. so for lists Python doesn't keep the same object in the
cache??? So in that case it is not interned & hence any objects
created will **point** to seperate area in memory as seen by your
m is s #( for lists)
False


Thanx again :)
[snip]

--
cheers,
Ishwor Gurung
Jul 18 '05 #3
Ishwor wrote:
On Sun, 05 Dec 2004 09:44:13 -0500, Kent Johnson <ke******@yahoo.com> wrote:
This behaviour is due to the way strings are handled. In some cases strings are 'interned' which
lets the interpreter keep only a single copy of a string. If you try it with a list you get a
different result
Thanx Kent. so for lists Python doesn't keep the same object in the
cache???


Right, AFAIK lists are not cached in this way.

Kent
Jul 18 '05 #4

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

Similar topics

3
by: ofer h | last post by:
hello all i have got a span inside a td of a table. i am trying to clip the string that exceeds the length of the td and then put three dots ("...") after it so it will show that the text was...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
3
by: Bryan Olson | last post by:
I recently wrote a module supporting value-shared slicing. I don't know if this functionality already existed somewhere, but I think it's useful enough that other Pythoners might want it, so here...
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
10
by: rtilley | last post by:
s = list('some_random_string') print s s.reverse() print s s = ''.join(s) print s Surely there's a better way to do this, right?
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
16
by: Scott | last post by:
Yeah I know strings == immutable, but question 1 in section 7.14 of "How to think like a computer Scientist" has me trying to reverse one. I've come up with two things, one works almost like it...
2
by: Rahul | last post by:
Hi Everyone, I was working around object slicing (pass by value) and was wondering how it is specified in the standard, class A { }; class B: public A
4
by: Ahmed, Shakir | last post by:
I need to remove text string from the list of the numbers mentioned below: 080829-7_A 070529-5_c 080824-7_O 070405_6_p The output will be : 080829-7 070529-5
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.