I am new to Python and want to create classes/structures similar to c++ but I have problems understanding what python actually does
from dataclasses import dataclass
class innerClass:
x: int
class outerClass:
a: innerClass
b: str
c: str
test_1 = outerClass()
def setTest(val):
global test_1
test_1.x=val
def getTest():
z=1
global test_1
if z==1:
setTest(z)
return [test_1.x]
getTest()
If I change the value of z to z=0
then it has the following issue
Traceback (most recent call last):
File "simplePy.py", line 27, in <module>
getTest()
File "simplePy.py", line 25, in getTest
return [test_1.x]
AttributeError: 'outerClass' object has no attribute 'x'
so I have two questions
- how does it access x with only test_1.x? I would expect it to be test_1.a.x as outerClass->innerClass->variable
- as it worked with
z=1
why does it complain about x not being an attribute withz=0
? I was expecting it to return either rubbish value or zero