cute use of lambda

P

Paul Rubin

Simon Forman said:
class FakeQueue(list):
put = list.append
get = lambda self: self.pop(0)

from collections import deque
class FakeQueue(deque):
put = deque.append
get = deque.popleft
 
T

Terry Reedy

| class FakeQueue(list):
| put = list.append
| get = lambda self: self.pop(0)

Sorry, to me this is a foolish use of lambda as it is exactly the same as

def get(self): self.pop(0)

except that in the latter, the resulting function object gets a proper
internal name instead of '<lambda>', therefore giving a more informative
traceback in case of an exception.

tjr
 
K

Kay Schluehr

| class FakeQueue(list):
| put = list.append
| get = lambda self: self.pop(0)

Sorry, to me this is a foolish use of lambda as it is exactly the same as

def get(self): self.pop(0)

Better use

def get(self): return self.pop(0)

otherwise you will *get* nothing ;)

BTW I wonder why a destructive operation is named *get* instead of
*take* ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top