How to pass a class variable as a default value in a static method in Python
问题 I want to pass a class variable as a default value to a static method. But when I import the class I get an error NameError: name 'MyClass' is not defined class MyClass: x = 100 y = 200 @staticmethod def foo(x = MyClass.x, y = MyClass.y): return x*y 回答1: MyClass is not defined yet when Python wants to bind the default arguments, but x and y are already defined in the classes' scope. In other words, you can write: class MyClass: x = 100 y = 200 @staticmethod def foo(x=x, y=y): return x*y Note