0

Could you please explain, why this code works in Python 3.6

class B:
    a = 'a_val'
    b = {'a': 'a_val: {val}'.format(val=a)}

but this doesn't work?

class C:
    a = 'a_val'
    b = {i: 'a_val: {val}'.format(val=a) for i in range(0,2)}
NameError: name 'a' is not defined

and both classes work in Python 2.6.

martineau
  • 112,593
  • 23
  • 157
  • 280
Dimaf
  • 613
  • 1
  • 10
  • 23

0 Answers0