Python define class functions in seperate py file -
i working on project deals data analysis. in order simplify process of finding , modifying functions, have decided put functions of similar purposes in different py files. issue comes in when functions in 1 py file require functions work. think can solve issue working class. demonstrate solution trying implement, have 3 files:
a.py
def a1(self): self.var=3 def a2(self): self.b1(4)
b.py
def b1(self, x): self.var=x def b2(self): self.a1()
testclass.py
class test(object): def __init__(self): self.var=0 b import * import * a=test() print a.var a.a1() print a.var a.a2() print a.var a.b2() print a.var
this want python unhappy me importing these 2 py files in fashion. in c++ prototype functions, forward declaration, , place definitions in file , have no issues. proper way define functions class in py file class definition not in?
use mix-in classes:
# in a.py class a(object): def a1(self): self.var=3 def a2(self): self.b1(4) # in b.py class b(object): def b1(self, x): self.var=x def b2(self): self.a1() # in testclass.py class test(a, b): def __init__(self): self.var=0
Comments
Post a Comment