what is lambda used for in real code?

Steven Bethard steven.bethard at gmail.com
Fri Dec 31 12:05:33 EST 2004


Alex Martelli wrote:
> Steven Bethard <steven.bethard at gmail.com> wrote:
>> 
>>py> class C(object):
>>...     def __init__(self):
>>...         self.plural = lambda n: int(n != 1)
>>...
>>py> c = C()
>>py> c.__class__.plural(1)
>>Traceback (most recent call last):
>>   File "<interactive input>", line 1, in ?
>>AttributeError: type object 'C' has no attribute 'plural'
>>py> c.plural(1)
>>0
> 
> 
> This shows that staticmethod has slightly wider applicability, yes, but
> I don't see this as a problem.  IOW, I see no real use cases where it's
> important that hasattr(C, 'plural') is false while hasattr(C(),
> 'plural') is true [I could of course be missing something!].

True, true.  I guess I was just wrapped up in reproducing the class 
behavior.  Making it available as a staticmethod of the class would of 
course only add functionality, not remove any.

Steve



More information about the Python-list mailing list