Reputation: 106
so I'm trying to use a method from an imported class, but I get an error. Here is a reproducible example:
file1.py
class A:
def __init__(self, A1, A2):
self.A1 = A1
self.A2 = A2
def add_A1_A2(self):
print(self.A1+self.A2)
file2.py
from file1 import A
A1=1
A2=2
A(A1, A2).add_A1_A2()
I get an error: name 'A1' is not defined
Upvotes: 1
Views: 37
Reputation: 4427
Works for me =P. Check that you don't have cached .pyc
files around.
$ cat file1.py
class A:
def __init__(self, A1, A2):
self.A1 = A1
self.A2 = A2
def add_A1_A2(self):
print(self.A1+self.A2)
$ cat file2.py
from file1 import A
A1=1
A2=2
A(A1, A2).add_A1_A2()
$ python3.7 file2.py
3
Upvotes: 3