and [True,True] --> [True, True]?????

Terry Reedy tjreedy at udel.edu
Mon Apr 27 14:10:44 EDT 2009


jazbees wrote:
> On Apr 25, 12:11 pm, Duncan Booth <duncan.bo... at invalid.invalid>
> wrote:
>> jazbees <jazb... at gmail.com> wrote:
>>>>>> hasvowels = lambda x:max([y in x for y in "aeiou"])
>>>>>> hasvowels("parsnips")
>>> True
>>>>>> hasvowels("sfwdkj")
>>> False
>> Do you object to using def to define functions?
> 
> Not at all.  Do you object to my use of lambdas?  I'm not aware of
> anything that says it's bad form to define a function using a lambda
> when the only thing that a function does is immediately return some
> calculated value.

The difference between

hasvowels = lambda x:max([y in x for y in "aeiou"])

and

def hasvowels(x): return max([y in x for y in "aeiou"])

is that the first is 4 chars shorter, but the result has a generic 
.__name__ attribute of '<lambda>' insteand of the specific 'hasvowels', 
which is definitely more useful.  Given this and the that the main 
purpose of lambda is to avoid a local name binding, many consider its 
use in 'name = lambda...' to be bad form.

tjr




More information about the Python-list mailing list