Newbie ?:A more concise way?

Geoff Gerrietts geoff at gerrietts.net
Tue Mar 5 03:04:34 EST 2002


Quoting Brian (brian at lodoss.org):
> Do you also feel my original code is better in terms of style or would
> it be even better as the more verbose:
> 
> z = []
> for i in xrange(y):
>     z.append(MyClass())

It might go faster. :) An alternative (though not a good one) might be
to make MyClass's __init__ accept *args and ignore them:

    class MyClass:
        def __init__(self, *args):
            pass

    instant_list = map(MyClass, range(y))

There's not much compelling about this solution, but it does get rid
of the extra function call.

> OK - now I have another question (and remember I'm new to Python) if
> map and lambda are part of the core language and have well defined
> behavior, why is it that using them is considered bad form?  Are they
> just the redheaded step-children of the language, or what? ;-)

An amusing but possibly apt observation. Through occassional glimpses
at conversations, a few attributed quotes, and a bunch of my own
guesswork, I would lay it out sorta like this:

map() and the other functionals were added because it's a convenient
form, a contributed patch, and popular among devotees of functional
languages. With built-in functions, they can still be quite peppy,
and there's no denying the power of expression they offer. But for
most tasks, it's like having a 25 lb sledge and a whole lot of
finishing nails to drive. It can get the job done, but it won't always
be pretty.

That said, a lot of people use them. Even when aware of the tradeoffs,
I use them, definitely a lot more than I should. List comprehensions
has reduced that somewhat.

I think that I have seen notables in the python community (I would
name names, but I don't have quotes to reference) have indicated
regret at the introduction of map() and friends into the language.
They end up being appropriate only very, very rarely.

The lambda construct doesn't suffer from quite the same stigma, though
I think some feel that it's overused, and to bad effect. The
consequence often seems to be code that's a harder to read and
understand rather than the other way around.

I don't think I'm terribly religious either direction on this issue;
in some ways, I'm like the contemporary smoker. I know functional
functions and excessive lambdas are bad for me, but I just can't quit.

I think there are a lot of us sinners in this particular layer of
Hell.

--G.

-- 
Geoff Gerrietts           "By doing just a little every day, I can gradually 
                           let the task completely overwhelm me." 
<geoff at gerrietts net>        --Ashleigh Brilliant




More information about the Python-list mailing list