For a.py:
class a():
def __init__(self,var = 0):
self.var = var
def p(self):
print(self.var)
For b.py:
import a
a.var=2
o=a()
o.p()
What I want is: 2.
But it shows: 0.
When I import the module, ta
, I want to use add_all_ta_features
in this module, which use other class in this module. Meanwhile, I want to change the variable in other class, but the result is still the same.
Could anyone help me with that? Thank you.
CodePudding user response:
For a.py:
class a():
def __init__(self,var = 0):
self.var = var
def p(self):
print(self.var)
For b.py
import a
ob=a.a(2)
## Or you can do
## ob=a.a()
## ob.var=2
p=ob.p()
In this case, your output will be 2, What exactly is happening that your class name and the python module name are the same. In here you have just imported the module, but when you create an object of any class you have to use the class name using the above method or by these methods.
## Method 2
from a import a
ob=a(2)
## Or you can do
## ob=a()
## ob.var=2
p=ob.p()
or
## Method 3
import a as obj
ob=obj.a(2)
## Or you can do
## ob=obj.a()
## ob.var=2
p=ob.p()
CodePudding user response:
Go ahead and initialise a
b.py
import a
an_a = a()
a.var = 23 #set whatever you want
print(a.var)