User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,596 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,441 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 630 | Replies: 6
Reply
Join Date: Jun 2007
Posts: 27
Reputation: StrikerX11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
StrikerX11 StrikerX11 is offline Offline
Light Poster

Creating a ref to a string ?

  #1  
Sep 8th, 2007
Hi,
How can i create a ref to a string ? it doesn't work like lists

List : (IDs are the same!)
>>> li=[1, 2, 3, 4, 5]
>>> li_ref=li
>>> id(li)
13545800
>>> id(li_ref)
13545800
>>> li.pop()
5
>>> li
[1, 2, 3, 4]
>>> id(li)
13545800
>>> li_ref
[1, 2, 3, 4]
>>> id(li_ref)
13545800

String : (the ID of each is different!)
>>> s="Hello, "
>>> s_ref=s
>>> id(s)
13553952
>>> id(s_ref)
13553952
>>> s += "World!"
>>> s
'Hello, World!'
>>> s_ref
'Hello, '
>>> id(s)
13560752
>>> id(s_ref)
13553952

So any ideas ?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Posts: 468
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 65
woooee woooee is offline Offline
Posting Pro in Training

Re: Creating a ref to a string ?

  #2  
Sep 8th, 2007
>>> a=b="test"
>>> print a
test
>>> print b
test
>>> id(a)
3084724384L
>>> id(b)
3084724384L

Or convert the string to a list and then back to a string when you are finished.
Reply With Quote  
Join Date: Sep 2007
Posts: 19
Reputation: paddy3118 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
paddy3118 paddy3118 is offline Offline
Newbie Poster

Re: Creating a ref to a string ?

  #3  
Sep 8th, 2007
Strings are known as immutable. You cannot take a string object and change its value without creating another string, so although you can have two names refer to the same string object, when you create a new string object and assign it to one of the names, the other name is still referring to the original object.

You could embed a string in a list and have two names refer to the list. Then changing the string in the list will be seen via both names
i.e:

s1 = ["Spam"]
s2 = s1
s2[0] = "Ham"
assert s1 == ["Ham"]

- Paddy.
Last edited by paddy3118 : Sep 8th, 2007 at 1:50 pm.
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Creating a ref to a string ?

  #4  
Sep 9th, 2007
or create a class that looks like a string:

  1. class MyString(object):
  2.  
  3. def __init__(self, string):
  4. self.value = string
  5.  
  6. def __str__(self):
  7. return self.value
  8.  
  9. # I think strings ought to support index assignment. So my strings will.
  10. def __setitem__(self, index, item):
  11. self.value = self.value[:index] + item + self.value[index+1:]
  12.  
  13. def __getitem__(self, index):
  14. return self.value[index]
  15.  
  16. def __mul__(self, multiple):
  17. return self.value * multiple
  18.  
  19. def __add__(self, addend):
  20. return self.value + addend
  21.  
  22. def __iadd__(self, addend):
  23. self.value += addend
  24. return self
  25.  
  26. # add more methods to make it even more string-like.
  27.  
  28. a = MyString("Hi!")
  29. print a, id(a)
  30. print a*5, id(a)
  31. a += "whoa!"
  32. print a, id(a)
  33. print a[1], id(a)
  34. a[2] = "?"
  35. print a, id(a)
  36.  
  37. ---
  38. Hi! 12077744
  39. Hi!Hi!Hi!Hi!Hi! 12077744
  40. Hi!whoa! 12077744
  41. i 12077744
  42. Hi?whoa! 12077744

But now this raises the question: why do you want such a thing?

Jeff
Reply With Quote  
Join Date: Sep 2007
Posts: 19
Reputation: paddy3118 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
paddy3118 paddy3118 is offline Offline
Newbie Poster

Re: Creating a ref to a string ?

  #5  
Sep 9th, 2007
Check out the mutable string class past the middle of this page
Reply With Quote  
Join Date: Jun 2007
Posts: 27
Reputation: StrikerX11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
StrikerX11 StrikerX11 is offline Offline
Light Poster

Re: Creating a ref to a string ?

  #6  
Sep 9th, 2007
Sorry for being late,
Thanks guyz, that was helpful
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Creating a ref to a string ?

  #7  
Sep 9th, 2007
Ooh, the MutableString class was interesting. Thanks, paddy. Of course, the various caveats on that page were not very encouraging...

Jeff
Last edited by jrcagle : Sep 9th, 2007 at 4:42 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 6:53 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC