So, I tried to search here and couldn't find any solution to that problem: I need to define a variable inside a "lambda".
I have that piece of code:
def z(t,s):exec("t=s")
r = type('', (), {
'__init__': lambda t,*s: z("t.c",s),
's': lambda t: (lambda t,*s: [t[::-1] for t in s])(*t.c),
'l': lambda t: (lambda t,*s: list(s[::-1]))(*t.c),
'd': lambda t: t.c
})
w = ["APPLE", "BEACH", "CITRUS"]
print r(*w).s()
print r(*w).l()
print r(*w).d()
Nothing works as it should work... Also when I call the function "d", it returns me this error:
AttributeError: '' object has no attribute 'c'
When it should return a tuple, something like:
('ALPHA', 'BRAVO', 'CHARLIE')
EDIT: Thanks guys, it works now, and is even smaller:
r=type('',(),{'__init__':lambda t,*s:setattr(t,'c',s),'s':lambda t:[t[::-1] for t in t.c],'l':lambda t:list(t.c[::-1]),'d':lambda t:t.c})
classstatement? And are you aware thatdefcan do literally anythinglambdacan?def z(t,s):exec("t=s");r=type('',(),{'c':'','__init__':lambda t,*s:z(t.c,s),'s':lambda t:(lambda t,*s:[t[::-1] for t in s])(*t.c),'l':lambda t:(lambda t,*s:list(s[::-1]))(*t.c),'d':lambda t:t.c})