(See final question at EDIT below. The original title was Why static var in static var is ok but static method not ok in Python?)
Original post: I have a Python question much related to Refering to static methods from static variables but not exactly the same. So code where one refers to a static method in a static variable, e.g.,
class A:
@staticmethod
def f(a):
return a*a
v = f(2) # A.f(2) does not work either
is invalid (and as far as I can tell same holds without @staticmethod as well). Fine. Yet, one can refer to another static variable without any problem:
class A:
i = 2
v = i * i # not using A.i here
print(A.v) # gives 4
What is the rationale behind the different treatment of a static variable and a static method here? I am trying from Python 3.6, if it matters.
EDIT:
The suggestion by gilch to think of it as a namespace indeed helps. Now I realize that my test case was too simplistic, however. What failed was initializing a list/dictionary for some reason:
class A:
def f(a): return a*a
i = f(2) #works, indeed
lst = [2, 3, 4]
lst2 = [ v*v for v in lst] #works
lst3 = [ f(v) for v in lst] #fails??
dct = { f(v) : v for v in lst } #fails??
Of course, the last two lines both work if f is defined outside A. So this may be a question of scope somehow...