The following is my demo program,
I want to create a temporary object to use in class declaration
But it said, NameError: name 'tmp' is not defined
I want to know why and how to do this
from dataclasses import dataclass
@dataclass
class A:
ID:int = 0
Flag1:bool = False
Flag2:bool = False
def A_to_dict(a:A):
return{"ID":a.ID,
"Flag1":a.Flag1,
'Flag2': a.Flag2}
class B:
tmp = A_to_dict(A())
# bvars = [x for x in tmp if type(tmp[x]) is bool] # NameError: name 'tmp' is not defined
bvars = [x for x in A_to_dict(A()) if type(A_to_dict(A())[x]) is bool] # OK
b = B()
print(b.bvars)