Duh v D'oh
Nigel Rantor
wiggly at wiggly.org
Fri Nov 7 12:44:40 GMT 2008
Paul Makepeace wrote:
> On Fri, Nov 7, 2008 at 11:48 AM, Peter Corlett <abuse at cabal.org.uk> wrote:
>
>> On Thu, Nov 06, 2008 at 05:00:16PM +0000, Nigel Rantor wrote:
>>> Paul Makepeace wrote:
>> [...]
>>>> If it's not staring out at you, you're possibly also a python
>> programmer.
>>>> Python here arguably DWIM better than perl.
>>> Does python give you the keys by default?
>> Sort of. In Python, "for" loops over values returned by an iterator, and a
>> hash implements that interface.
But by interface here we just mean it implements certain named methods
that have certain semantics that everyone agrees make it an
iterator...right?
I mean, it's like C++ and templates, we duck type things rather than
actually implement interfaces as you are required to do in Java.
> The interface is pretty clean,
>
> my_dict = {'a': 1, 'b': 2}
> for k in my_dict:
> print k
>
> gives,
> a
> b
>
> for k, v in my_dict.items():
> print k, v
>
> gives,
> a 1
> b 2
>
> (ObPedantry, .iteritems() is preferred since it evaluates lazily with a
> generator.)
Yes, well, Perl has 'each' for iterating over pairs, so they look almost
identical. The main difference I can see is that python defaults to
assuming you want the keys of a hash when iterating rather than the values.
It's a fair enough design decision, but like any decision it's not
always going to be the right thing. But yes, I agree, it is kind of
DWIMier than iterating over both keys and values for a hash.
If you look around though, lots of languages do this better than Perl
these days. I was going to mention some languages but I can't be arsed
with the inevitable flaming session.
n
p.s. I'm still reading the python list for a laugh. they just had a
*HUGE* rant/discussion about what "rebinding" means and how you describe
it to people coming from other languages.
More information about the london.pm
mailing list