Re: Mutable strings - symetry with list types
Dennis Lee Bieber wrote:
[color=blue]
> Prior to the creation of string methods, you'd have done
>
> import string
>
> ... string.join(bla h, ' ')
>
>[/color]
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.
[color=blue][color=green]
>>not obvious and it looks like a hack, IMO. Plus you can't do
>>somestring = '%s %s %s' % [ 'nine', 'bladed', 'sword' ][/color]
>
>
> 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"])[/color]
I just found it strange that you couldn't do it directly without
'casting'...Pro bably 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.
[color=blue]
>
> 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...).
>
>[color=green][color=darkred]
>>>>weapon = {"type":"Sword" , "attribute":"bl aded", "modifier":"nin e"}
>>>>weapon[/color][/color]
>
> {'attribute': 'bladed', 'modifier': 'nine', 'type': 'Sword'}
>[color=green][color=darkred]
>>>>somestrin g = "%(modifier )s %(attribute)s %(type)s" % weapon
>>>>somestrin g[/color][/color]
>
> 'nine bladed Sword'
>[/color]
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.
Dennis Lee Bieber wrote:
[color=blue]
> Prior to the creation of string methods, you'd have done
>
> import string
>
> ... string.join(bla h, ' ')
>
>[/color]
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.
[color=blue][color=green]
>>not obvious and it looks like a hack, IMO. Plus you can't do
>>somestring = '%s %s %s' % [ 'nine', 'bladed', 'sword' ][/color]
>
>
> 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"])[/color]
I just found it strange that you couldn't do it directly without
'casting'...Pro bably 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.
[color=blue]
>
> 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...).
>
>[color=green][color=darkred]
>>>>weapon = {"type":"Sword" , "attribute":"bl aded", "modifier":"nin e"}
>>>>weapon[/color][/color]
>
> {'attribute': 'bladed', 'modifier': 'nine', 'type': 'Sword'}
>[color=green][color=darkred]
>>>>somestrin g = "%(modifier )s %(attribute)s %(type)s" % weapon
>>>>somestrin g[/color][/color]
>
> 'nine bladed Sword'
>[/color]
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.
Comment