Re: function that modifies a string

2006-07-11 Thread Diez B. Roggisch
> I'm sorry, perhaps I'm being slow today, but just why are they dangerous? > More dangerous than, say, mutable lists and mutable dicts? Unless I'm > missing something, the worst that can happen is that people will write > inefficient code, and they'll be caught out by the same sort of things > tha

Re: function that modifies a string

2006-07-10 Thread Steven D'Aprano
On Mon, 10 Jul 2006 19:30:09 +0200, Diez B. Roggisch wrote: [snip] > So: Yes, mutable strings are dangerous. I'm sorry, perhaps I'm being slow today, but just why are they dangerous? More dangerous than, say, mutable lists and mutable dicts? Unless I'm missing something, the worst that can hap

Re: function that modifies a string

2006-07-10 Thread Diez B. Roggisch
>>> Arrays of chars are dangerous. If you insist, use Python lists of >>> Python "chars" (strings of length 1). >> Why are they more dangerous than a self-written mutable string? > > I didn't say that. I meant that arrays in the C++ sense are dangerous. So what? Python's arrays are backed by arra

Re: function that modifies a string

2006-07-10 Thread Fredrik Lundh
tac-tics wrote: > I didn't say that. I meant that arrays in the C++ sense are dangerous. so what do you think Python's string type uses on the inside ? > C++ offers pass by value options. That makes it so you never need to > worry about messing up data that doesn't belong to you unless you > exp

Re: function that modifies a string

2006-07-10 Thread tac-tics
> >> What's wrong about arrays of chars? > > > > Arrays of chars are dangerous. If you insist, use Python lists of > > Python "chars" (strings of length 1). > > Why are they more dangerous than a self-written mutable string? I didn't say that. I meant that arrays in the C++ sense are dangerous.

Re: function that modifies a string

2006-07-10 Thread Jason
Simon Forman wrote: > greenflame wrote: > > Jason wrote: > > > > > > There /are/ a few hacks which will do what you want. However, if you > > > really need it, then you probably need to rethink your program design. > > > Remember, you can't change a string since a string is immutable! You > > > c

Re: function that modifies a string

2006-07-10 Thread Steven D'Aprano
On Mon, 10 Jul 2006 16:27:03 +0200, Diez B. Roggisch wrote: >> Of course, another right way would be to have mutable strings in Python. >> I understand why strings need to be immutable in order to work with dicts, >> but is there any reason why (hypothetical) mutable strings should be >> avoided i

Re: function that modifies a string

2006-07-10 Thread Steven D'Aprano
On Mon, 10 Jul 2006 15:23:36 +0100, Sion Arrowsmith wrote: > Steven D'Aprano <[EMAIL PROTECTED]> wrote: >>Of course, another right way would be to have mutable strings in Python. > > What significant advantage would mutable strings have over StringIO > and wrapping list manipulation in list(s) a

Re: function that modifies a string

2006-07-10 Thread Diez B. Roggisch
tac-tics wrote: > > Diez B. Roggisch wrote: >> > Of course, another right way would be to have mutable strings in >> > Python. I understand why strings need to be immutable in order to work >> > with dicts, but is there any reason why (hypothetical) mutable strings >> > should be avoided in situa

Re: function that modifies a string

2006-07-10 Thread tac-tics
Diez B. Roggisch wrote: > > Of course, another right way would be to have mutable strings in Python. > > I understand why strings need to be immutable in order to work with dicts, > > but is there any reason why (hypothetical) mutable strings should be > > avoided in situations where they aren't n

Re: function that modifies a string

2006-07-10 Thread Diez B. Roggisch
> Of course, another right way would be to have mutable strings in Python. > I understand why strings need to be immutable in order to work with dicts, > but is there any reason why (hypothetical) mutable strings should be > avoided in situations where they aren't needed as dictionary keys? Python

Re: function that modifies a string

2006-07-10 Thread Sion Arrowsmith
Steven D'Aprano <[EMAIL PROTECTED]> wrote: >Of course, another right way would be to have mutable strings in Python. What significant advantage would mutable strings have over StringIO and wrapping list manipulation in list(s) and ''.join(l). Other than that pleasing symmetry with sets/frozensets

Re: function that modifies a string

2006-07-10 Thread Steven D'Aprano
On Sun, 09 Jul 2006 21:31:00 -0700, placid wrote: > > greenflame wrote: >> I want to make a function that does the following. I will call it >> thefunc for short. >> >> >>> s = "Char" >> >>> thefunc(s) >> >>> s >> '||Char>>' >> >> I tried the following >> >> def thefunc(s): >> s = "||" + s +

Re: function that modifies a string

2006-07-09 Thread Simon Forman
greenflame wrote: > Jason wrote: > > > > There /are/ a few hacks which will do what you want. However, if you > > really need it, then you probably need to rethink your program design. > > Remember, you can't change a string since a string is immutable! You > > can change a variable to bind to an

Re: function that modifies a string

2006-07-09 Thread Simon Forman
placid wrote: > quick hack > > def thefunc(s): > return s = "||" + s + ">>" >>> def thefunc(s): return s = "||" + s + ">>" SyntaxError: invalid syntax -- http://mail.python.org/mailman/listinfo/python-list

Re: function that modifies a string

2006-07-09 Thread greenflame
Jason wrote: > > You cannot do what you are trying to do directly. Strings are > immutable objects. Once a string is created, that string cannot be > modified. When you operate on a string, you produce a different > string. Functions which operate on a string should return their value: > > >>>

Re: function that modifies a string

2006-07-09 Thread placid
greenflame wrote: > I want to make a function that does the following. I will call it > thefunc for short. > > >>> s = "Char" > >>> thefunc(s) > >>> s > '||Char>>' > > I tried the following > > def thefunc(s): > s = "||" + s + ">>" > > The problem is that if I look at the string after I apply

Re: function that modifies a string

2006-07-09 Thread Jason
greenflame wrote: > I want to make a function that does the following. I will call it > thefunc for short. > > >>> s = "Char" > >>> thefunc(s) > >>> s > '||Char>>' > > I tried the following > > def thefunc(s): > s = "||" + s + ">>" > > The problem is that if I look at the string after I apply t

function that modifies a string

2006-07-09 Thread greenflame
I want to make a function that does the following. I will call it thefunc for short. >>> s = "Char" >>> thefunc(s) >>> s '||Char>>' I tried the following def thefunc(s): s = "||" + s + ">>" The problem is that if I look at the string after I apply the function to it, it is not modified. I r