0
x = 1

class Girl:
    x = 2
    print([i * x for i in (1, 2, 3)])

The output is [1, 2, 3].

So class does not have its own scope; someone told me that according to the LEGB rule, class body != closure, so Python looks for the global x.

I still have trouble understanding this. Are class bodies actually function bodies? You can actually run any code (if - else, for loop, etc) in that body, why? I'm so confused

init 1
  • 39
  • 6

0 Answers0