Python: why can't access a variable in a class?

A simple example:

class A(): a = 1 b = [a + i for i in range(3)] 

He will raise

NameError: name 'a' not defined

but

 class A(): a = 1 b = a + 2 

work.

What is the basic access rule in Python3 ?

+5
source share

Source: https://habr.com/ru/post/1245324/


All Articles