•
•
•
•
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
![]() |
•
•
Join Date: Jun 2007
Posts: 27
Reputation:
Rep Power: 2
Solved Threads: 1
Hi,
How can i create a ref to a string ? it doesn't work like lists
List : (IDs are the same!)
String : (the ID of each is different!)
So any ideas ?
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 ?
•
•
Join Date: Sep 2007
Posts: 19
Reputation:
Rep Power: 2
Solved Threads: 2
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.
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.
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
or create a class that looks like a string:
But now this raises the question: why do you want such a thing?
Jeff
Python Syntax (Toggle Plain Text)
class MyString(object): def __init__(self, string): self.value = string def __str__(self): return self.value # I think strings ought to support index assignment. So my strings will. def __setitem__(self, index, item): self.value = self.value[:index] + item + self.value[index+1:] def __getitem__(self, index): return self.value[index] def __mul__(self, multiple): return self.value * multiple def __add__(self, addend): return self.value + addend def __iadd__(self, addend): self.value += addend return self # add more methods to make it even more string-like. a = MyString("Hi!") print a, id(a) print a*5, id(a) a += "whoa!" print a, id(a) print a[1], id(a) a[2] = "?" print a, id(a) --- Hi! 12077744 Hi!Hi!Hi!Hi!Hi! 12077744 Hi!whoa! 12077744 i 12077744 Hi?whoa! 12077744
But now this raises the question: why do you want such a thing?
Jeff
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Creating a Basic String Database (C++)
- new and in need (ASP)
- need help in creating class string (C++)
Other Threads in the Python Forum
- Previous Thread: Reading CSV files
- Next Thread: tab-delimited data



Linear Mode