Reputation: 885
I'm trying to create a constant file in python and i need to use a nested classes to declare some static variables within. Some of those variables use another variables from another inner class the problem is when i try to call that static variable a NameError shows up "name 'MyClass1' is not defined. this is an example:
class MyOutterClass:
class MyClass1:
myStaticVar1 = 5
class myClass2:
myStaticVar2 = MyClass1.myStaticVar1 * 2
the error message is in myStaticVar2 : NameError: name 'myClass1' is not defined.
do you guys have any solution or any 'work around' way to solve this problem and could you please tell me what causes this problem! Thanks.
Upvotes: 0
Views: 433
Reputation: 251548
Variables in class scope are not accessible within other scopes nested in the class scope. In your example, MyClass1
is a variable in the class scope of MyOuterClass
. Within the body of MyClass2
a new scope is created which does not have access to the enclosing class's scope, hence your error.
I don't really see a reason to use nested classes for this. You can just use dictionaries, or some kind of dictionary that provides attribute-style access (such as types.SimpleNamespace
.
Upvotes: 1