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
  • 119,623
  • 25
  • 170
  • 301
Dimaf
  • 653
  • 1
  • 11
  • 25

0 Answers0