I have the module Test.py with class test inside the module. Here is the code:
class test:
SIZE = 100;
tot = 0;
def __init__(self, int1, int2):
tot = int1 + int2;
def getTot(self):
return tot;
def printIntegers(self):
for i in range(0, 10):
print(i);
Now, at the interpreter I try:
>>> import Test
>>> t = test(1, 2);
I get the following error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
t = test(1, 2);
NameError: name 'test' is not defined
Where did I go wrong?
You have to access the class like so:
If you want to access the class like you tried to before, you have two options:
This imports everything from the module. However, it isn’t recommended as there is a possibility that something from the module can overwrite a builtin without realising it.
You can also do:
This is much safer, because you know which names you are overwriting, assuming you are actually overwriting anything.