Mutable strings - symetry with list types
Gordon Airport
uce at ftc.gov
Mon Sep 22 17:46:35 EDT 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.
More information about the Python-list
mailing list