我在多个文件中有多个类。例如,
档案1:
class gen_list ():
def gen_list_spice(self):
...档案2:
class gen_postsim ():
def gen_postsim(self):
...我想用另一个像这样的类来包装,
class char ()
def __init__ (self, type):
if (type == list):
....... (load gen_list only<-- this part i do not know how to write)
else
....... (load both)在最上面的包装器中,例如,如果我给list,我将能够使用gen_list_spice,否则,当我只需要调用object char时,就可以同时使用gen_list_spice和gen_postsim。
发布于 2018-11-20 09:25:30
我不知道为什么要这样做,但是您可以在文件的任何部分导入一个文件。
文件1,应该命名为get_list.py
class ListGenerator():
def gen_list_spice(self):
pass文件2,应该命名为gen_postsim.py
class PostsimGenerator():
def gen_postsim(self):
pass在包装文件中:
class char():
def __init__(self, type):
if type == list:
from gen_list import ListGenerator
gl = ListGenerator()
gl.gen_list_spice()
else:
from gen_postsim import PostsimGenerator
from gen_list import ListGenerator
gp = PostsimGenerator()
gp.gen_postsim()但这么做不是个好办法。您可以使用函数而不是类,并将它们导入文件头中。
文件中的gen_list.py
def gen_list_spice():
print("get list")
pass文件中的gen_postsim.py
def gen_postsim():
print("gen postsim")
pass在包装文件中
from gen_list import gen_list_spice
from gen_postsim import gen_postsim
class char():
def __init__(self, type):
if type == list:
gen_list_spice()
else:
gen_list_spice()
gen_postsim()https://stackoverflow.com/questions/53389419
复制相似问题