Equivalents of Ruby's "!" methods?

Simon Mullis simon at mullis.co.uk
Mon Aug 25 09:32:21 EDT 2008


Thanks to all for the quick responses.

2008/8/25 Ben Finney <bignose+hates-spam at benfinney.id.au>:

> This is a 'dict' instance in Python. A 'hash' is a different concept.
>
>> In order to strip the dict values in Python I (think) I can only do
>> something like:
>>
>> for k,v in h.items:
>>     h[k] = v.strip()
>
> The above won't do what you describe, since 'h.items' evaluates to
> that function object, which is not iterable. If you want the return
> value of the function, you must call the function:
>
>    for (k, v) in h.items():
>        h[k] = v.strip()
>

Yes - absolutely, a typo on my part.

> This will create a new value from each existing value, and re-bind
> each key to the new value for that key. Clear, straightforward, and
> Pythonic.
>
> You can also create a new dict from a generator, and re-bind the name
> to that new dict:
>
>    h = dict(
>        (k, v.strip())
>        for (k, v) in h.items())
>
> Also quite Pythonic, but rather less clear if one hasn't yet
> understood generator expressions. Very useful to have when needed,
> though.
>

Thanks for this - I'll have a look!

>> While in Ruby - for the equivale dict/hash - I have the option of an
>> in-place method:
>>
>> h.each_value { |v| val.strip! }
>>
>> Are there Python equivalents to the "!" methods in Ruby?
>
> I'm not overly familiar with Ruby, but the feature you describe above
> seems to rely on mutating the string value in-place. Is that right?
>

There are a number of methods that can be used to change things
"in-place" such as:

>> String.new().grep_methods("!")
=> ["upcase!", "gsub!", "downcase!", "chop!", "capitalize!", "tr!",
"chomp!", "swapcase!", "tr_s!", "succ!", "strip!", "delete!",
"lstrip!", "squeeze!", "next!", "rstrip!", "slice!", "reverse!",
"sub!"]

Or,

>> Array.new().grep_methods("!")
=> ["map!", "shuffle!", "uniq!", "reject!", "compact!", "slice!",
"sort!", "flatten!", "collect!", "reverse!"]

They normally have a non-"!" partner which is used only for a return
value and does not affect the original object.

But! This isn't a Ruby group so I'll stop now... ;-)

> Strings in Python are immutable (among other reasons, this allows them
> to meet the requirement of dict keys to be immutable, which in turn
> allows dict implementations to be very fast), so you can only get a
> new value for a string by creating a new string instance and re-bind
> the reference to that new value.

Normally I would use a Ruby symbol as a hash key:

h = { :key1 => "val1", :key2 => "val2" }

>From the stdlib docs:

"The same Symbol object will be created for a given name or string for
the duration of a program's execution, regardless of the context or
meaning of that name. Thus if Fred is a constant in one context, a
method in another, and a class in a third, the Symbol :Fred will be
the same object in all three contexts. "

There is no equivalent in Python (as far as I know, and I'm only in my
second week of Python so I'm more than likely incorrect!).

If you're interested:
http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol

Thanks again for the pointers.

-- 
Simon Mullis
_________________
simon at mullis.co.uk



More information about the Python-list mailing list