0

Why does exec fail to run this code, when it's called inside of a class method?

class test(object):
    def run(self):
        exec("f=lambda n:(n>15and f(n//16)or'')+'HBKD'[n//4%4]+'OAEI'[n%4];print(f(156))")

exec("g=lambda n:(n>15and g(n//16)or'')+'HBKD'[n//4%4]+'OAEI'[n%4];print(g(156))")
t = test()
t.run()

The exec call outside of the class works properly, but the call inside the run() function fails, and gives an NameError: 'NameError: name 'f' is not defined'. Why's that?

Yytsi
  • 424
  • 1
  • 5
  • 14

0 Answers0