Hi everyone,
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
Thanks,
Sebastian 8 1738 s0****@gmail.co m writes:
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
If there were, it would not be immutable. The 'str' type has only
immutable values.
You could implement your own string type, and have it allow mutable
values. You'd have to take care of creating those values explicitly,
though.
--
\ “Pinky, are you pondering what I'm pondering?” “I think so, |
`\ Brain, but shouldn't the bat boy be wearing a cape?” —_Pinky |
_o__) and The Brain_ |
Ben Finney
Sebastian:
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
No, but you can use other kind of things:
>>s = "hello" sl = list(s) sl[1] = "a" sl
['h', 'a', 'l', 'l', 'o']
>>"".join(sl)
'hallo'
>>from array import array sa = array("c", s) sa
array('c', 'hello')
>>sa[1] = "a" sa
array('c', 'hallo')
>>sa.tostring ()
'hallo'
Bye,
bearophile s0****@gmail.co m wrote:
Hi everyone,
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
In 3.0, ascii chars and encoded unicode chars in general can be stored
in a mutable bytearray.
On Jul 15, 3:06*pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
wrote:
s0s...@gmail.co m writes:
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
If there were, it would not be immutable. The 'str' type has only
immutable values.
You could implement your own string type, and have it allow mutable
values. You'd have to take care of creating those values explicitly,
though.
You could write a C extension which modifies strings, but that would
be a Bad Idea. s0****@gmail.co m wrote:
Hi everyone,
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
Thanks,
Sebastian
Why would you care? Just create a new string (with the changed contents) and
let garbage collection take care of the old one when all the references to it
have gone away. Since these types of questions seem to appear almost every day
on this list, this Python stuff is so much different than old languages people
have hard time making the conceptual "jump". You can basically quite worrying
about how/where things are stored, they just are.
-Larry
On Jul 15, 6:46 pm, Larry Bates <larry.ba...@we bsafe.com`wrote :
s0s...@gmail.co m wrote:
Hi everyone,
I've heard that a 'str' object is immutable. But is there *any* way to
modify a string's internal value?
Thanks,
Sebastian
Why would you care? Just create a new string (with the changed contents) and
let garbage collection take care of the old one when all the references to it
have gone away. Since these types of questions seem to appear almost every day
on this list, this Python stuff is so much different than old languages people
have hard time making the conceptual "jump". You can basically quite worrying
about how/where things are stored, they just are.
Thanks for the reply. It's not that I'm having a hard time learning
Python. I've been programming it for some time. I just came across
this unusual situation where I'd like to modify a string passed to a
function, which seems impossible since Python passes arguments by
value. (Whereas in C, you'd customarily pass a pointer to the first
character in the string.)
I was playing around trying to simulate C++-like stream operations:
import sys
from os import linesep as endl
class PythonCout:
def __lshift__(self , obj):
sys.stdout.writ e(str(obj))
return self
def __repr__(self):
return "<cout>"
cout = PythonCout()
cout << "hello" << endl
But then trying to simulate cin:
class PythonCin:
def __rshift__(self , string):
string = sys.stdin.readl ine() # which doesn't make sense
line = ""
cin >line
And there goes the need to modify a string. :) s0****@gmail.co m writes:
I just came across this unusual situation where I'd like to modify a
string passed to a function
Again: Why? The normal way to do this is to create a new string and
return that.
which seems impossible since Python passes arguments by value.
No, Python passes arguments by reference
<URL:http://effbot.org/zone/python-objects.htm>.
--
\ “For of those to whom much is given, much is required.” —John |
`\ F. Kennedy |
_o__) |
Ben Finney
On Jul 15, 11:55 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
wrote:
s0s...@gmail.co m writes:
I just came across this unusual situation where I'd like to modify a
string passed to a function
Again: Why? The normal way to do this is to create a new string and
return that.
<snip>
Yes, usually, but that won't work in this case (look at my previous
post around the 'cin >line' line). This is what I came up with:
class StreamLineBuffe r:
def __init__(self):
self.buf = ""
def __rrshift__(sel f, stream):
self.buf = stream.readline ()
def __str__(self):
return self.buf
cin = sys.stdin
buf = StreamLineBuffe r()
cin >buf
line = str(buf)
Works like a charm :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jwpioneer |
last post by:
I have a need within an application to modify the path environment variable,
as I need to find specific directories and remove them. I use the following
code to do this:
RegistryKey rkey = null;
rkey =
Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session
Manager\Environment",true);
string p = (String)rkey.GetValue("Path");
string pp = p.Split(';');
|
by: Markus Dehmann |
last post by:
I guess this is a kind of newbie question (since most pointer questions are
newbie questions).
In the program below, modify(string* s) is supposed to change the content
that s points to. But the main() function shows that nothing is changed at
all!
What am I doing wrong?
|
by: Glenn |
last post by:
Searching through google groups, I have not found anyone able to modify
the query string of a request. I was hoping to make the query string
as short as possible so that a search with many criteria could be
bookmarked. I had planned to have the query string be
key=value for each criteria
But with dropdowns, the default choice is always submitted. There
were other parameters that I had to use that were longer than I hoped.
And...
|
by: Charles Sullivan |
last post by:
I'm working on a program which has a "tree" of command line arguments,
i.e.,
myprogram level1 ]]
such that there can be more than one level2 argument for each level1
argument and more than one level3 argument for each level2 argument, etc.
Suppose I code it similar to this fragment:
int main( int argc, char *argv )
{
|
by: Vladimir C. |
last post by:
Hashtable map = new Hashtable();
map = 10;
map = 20;
foreach(DictionaryEntry e in map)
{
e.Value = 100;
Console.WriteLine("{0}: {1}", key, map);
}
| |
by: Fabrice |
last post by:
Hello
Is it possible to modify CommandArgument property of a LinkButton from vb
class (code behind).
So, this is my control in my .aspx page :
<asp:LinkButton id="strTd" OnCommand="lnkButtonClick"
enableviewstate="False" CssClass="Detail" Text="Text"
runat="server"></asp:LinkButton>
|
by: Bob |
last post by:
Hi all,
I'm trying to import data, modify the data then insert it into a new
table. The code below works fine for it but it takes a really long
time for 15,000 odd records. Is there a way I can speed up the
processing substantially? as it currently takes about 10 minutes and
thats just way too long because there is many of these imports that I
need to do.... I currently insert each record one by one and I
imagine
thats where all the...
|
by: wizofaus |
last post by:
Given the following code:
public class Test
{
static unsafe void StringManip(string data)
{
fixed (char* ps = data)
ps = '$';
}
|
by: Ramesh |
last post by:
Hello,
I am using the ofstream class to create a text file with keys and
values like:
Key1=Value10
Key2=Value15
Key3=Value20
In case I need to set a new value for Key2, say value50 - I am able to
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |