Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Mutable strings

Reply
Thread Tools

Mutable strings

 
 
Jeff Epler
Guest
Posts: n/a
 
      09-22-2003
On Mon, Sep 22, 2003 at 12:31:58PM +0000, Alex Martelli wrote:
> But, there IS one! So, hat's wrong with it...?!


People seem to love to have literals for things. Otherwise, they feel
that a type is second-class.

Jeff

 
Reply With Quote
 
 
 
 
logistix at cathoderaymission.net
Guest
Posts: n/a
 
      09-22-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Hans-Joachim Widmaier) wrote in message news:<(E-Mail Removed). com>...
> Andy Jewell <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>
> > Mutable strings are one thing that I missed, initially, when I first star
> > ted
> > using Python. After a while, as the "Pythonic" way of doing things sank
> > in,
> > I realised that Python doesn't *need* mutable strings.

>
> Mutable strings come to *my* mind whenever I have to play with huge
> binary data. Working with tens of megabytes is inherently somewhat
> slow.
>


import array
x = arrray.array('c')

Pretty much creates a mutable string for these cases, although the
interface is a little different.
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      09-22-2003
On Monday 22 September 2003 02:50 pm, Jeff Epler wrote:
> On Mon, Sep 22, 2003 at 12:31:58PM +0000, Alex Martelli wrote:
> > But, there IS one! So, hat's wrong with it...?!

>
> People seem to love to have literals for things. Otherwise, they feel
> that a type is second-class.


Sure. I have no problem deeming "mutable strings" (array of bytes)
to be "second-class" in some vague sense, since their use is so rare
and the need for literals of that type even rarer; lacking literals for,
e.g., sets.Set "troubles" me far more.

I do keep daydreaming of some "user-defined semiliteral syntax"
such as, e.g. <identifier>{<balanced-parentheses tokens>} to
result in a call to (e.g.) <identifier>.__literal__ with a list (or other
sequence) of tokens as the argument, returning whatever that
call returns. But perhaps it isn't that good an idea after all (it
does imply the __literal__ classmethod or staticmethod doing
some sort of runtime compilation and execution of those tokens,
and opens the doors to the risk of some seriously nonPythonic
syntax for such "literals-initializers").


Alex


 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      09-22-2003
Look at PEP's 296 and 298.

John Roth

"Gordon Airport" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Peter Hansen wrote:
>
> snip good points that I suspect could be handled with a fairly simple
> regex
>
> > It might be worth your writing a PEP, however, if only so that the
> > idea could be killed and buried for good.
> >

>
> Yeah, I didn't see one already but I kind of expected this response.
> Still, it didn't put a stake in the heart of the ternary operator
> issue.
>
>



 
Reply With Quote
 
Gordon Airport
Guest
Posts: n/a
 
      09-22-2003
Peter Hansen wrote:
> Gordon Airport wrote:
>
>>Peter Hansen wrote:
>>
>>snip good points that I suspect could be handled with a fairly simple
>>regex

>
>
> I'd argue the point, but I guess until you try it, we'll never know. <wink>
>


Okay, I've tried it and I'll chalk it up to my inexperience with regular
expressions and sed, but I don't have anyhting to show. The general
strategy , though, is to make several passes; first escape all inner
strings, then convert all outer string delimiters (now the only ones not
escaped) to the immutable symbol. I feel like I'll wake up at 2 a.m.
with the answer, but I'll post now anyway.
(Assuming you /can/ say "every instance of A between B's" in regex...you
could always do it with a python script)

<snip>
>
>
> Apparently it served its purpose quite well. The main problem before
> the PEP and vote was that there was no PEP to point to when somebody
> asked about it, so you could say "asked and answered... will not happen".
>
> Now there is, and the few times the issue has come up since, someone
> has fairly quickly pointed to the PEP each time, avoiding lengthier
> discussion.
>
> -Peter


Fair enough. Maybe I will submit a PEP for this, I've never looked into
what's involved.

 
Reply With Quote
 
Gordon Airport
Guest
Posts: n/a
 
      09-22-2003
Dennis Lee Bieber wrote:

> Prior to the creation of string methods, you'd have done
>
> import string
>
> ... string.join(blah, ' ')
>
>


Yes, it looks even worse that way. I guess that it's just rare to use a
literal in the code as an object...I'm having trouble thinking of other
situations where you use the ability, but I won't pretend to be an
expert in the language.

>>not obvious and it looks like a hack, IMO. Plus you can't do
>>somestring = '%s %s %s' % [ 'nine', 'bladed', 'sword' ]

>
>
> If you know both sides have equal numbers of terms (the %s matches the
> number of entries in the list) you /can/ do a minor modification to
> that line:
>
> somestring = "%s %s %s" % tuple(["nine", "bladed", "sword"])


I just found it strange that you couldn't do it directly without
'casting'...Probably doesn't come up much anyway. Now that I think about
it it's an assignment so it's not really relevant to the discussion of
mutable strings.

>
> Of course, you could also create a dictionary and store those as
> attributes (though to my mind, you have a sword with one modifier
> "nine-bladed"; as is it could be interpreted to mean nine
> bladed-sword(s) -- though all swords are bladed...).
>
>
>>>>weapon = {"type":"Sword", "attribute":"bladed", "modifier":"nine"}
>>>>weapon

>
> {'attribute': 'bladed', 'modifier': 'nine', 'type': 'Sword'}
>
>>>>somestring = "%(modifier)s %(attribute)s %(type)s" % weapon
>>>>somestring

>
> 'nine bladed Sword'
>


All very handy, but I don't see how it could be done better with mutable
strings. I need to come up with some examples of applications.

 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      09-23-2003
Rob Tillotson:
> There already is one: array. Mutable blocks of bytes (or shorts,
> longs, floats, etc.), usable in many places where you might otherwise
> use a string (struct.unpack, writing to a file, etc.).


Even regular expressions

>>> import array, re
>>> t = "When in the course of human events"
>>> s = array.array("c", t)
>>> pat = re.compile(r"([aeiou]{2,})")
>>> m = pat.search(s)
>>> m.group(1)

array('c', 'ou')
>>>


Andrew
(E-Mail Removed)


 
Reply With Quote
 
Hans-Joachim Widmaier
Guest
Posts: n/a
 
      09-25-2003
Am Mon, 22 Sep 2003 15:26:56 +0200 schrieb Alex Martelli:

> On Monday 22 September 2003 02:50 pm, Jeff Epler wrote:
>> On Mon, Sep 22, 2003 at 12:31:58PM +0000, Alex Martelli wrote:
>> > But, there IS one! So, hat's wrong with it...?!

>>
>> People seem to love to have literals for things. Otherwise, they feel
>> that a type is second-class.

>
> Sure. I have no problem deeming "mutable strings" (array of bytes)
> to be "second-class" in some vague sense, since their use is so rare
> and the need for literals of that type even rarer; lacking literals for,
> e.g., sets.Set "troubles" me far more.
>
> I do keep daydreaming of some "user-defined semiliteral syntax"
> such as, e.g. <identifier>{<balanced-parentheses tokens>} to
> result in a call to (e.g.) <identifier>.__literal__ with a list (or other
> sequence) of tokens as the argument, returning whatever that
> call returns. But perhaps it isn't that good an idea after all (it
> does imply the __literal__ classmethod or staticmethod doing
> some sort of runtime compilation and execution of those tokens,
> and opens the doors to the risk of some seriously nonPythonic
> syntax for such "literals-initializers").


[Sorry for replying so late]

After writing what I did, I kept thinking about the issue. I finally
realized the same thing - it wasn't so much the missing datatype, as you
can use array but the missing literals. Having to construct constant
values at runtime doesn't strike me as nice. I'm coming from the embedded
world (hmm, that's not entirely true, as I'm not leaving it), and doing
something efficiently is a big concern there, so doing something at
runtime what you could do upfront is considered a bad thing.

Python doesn't have and cannot be the perfect language for just
everything. But even without "mutable strings", why does it have to be so
handy even for manipulating binaries then?

I'll get over it and give array a try.

Thanks to Jeff for finding the gist of it and Alex for his analysis.
It helps.

Hans-J.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mutable Strings - Any libraries that offer this? casebash Python 9 07-27-2009 12:36 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Are JavaScript strings mutable? Water Cooler v2 Javascript 12 04-20-2006 07:50 PM
Mutable strings Mystifier Ruby 19 01-14-2005 10:00 PM
RE: Mutable strings SBrunning@trisystems.co.uk Python 1 09-22-2003 09:31 PM



Advertisments