我不能使用主模块中另一个模块中的功能
#MAIN FILE - mainfile.py
#Imports / display / pygame.init
from file2 import *
font=pygame.font.SysFont("Arial",35)
example(1) #THERE I DEFINE DE CLASS
Text=font.render(list1[0],0,(255,255,255))
win.blit(Text,(5,5))
#while loop
#file 2 - file2.py
class example:
def __init__(self, whichlist):
global list1
if whichlist==1:
list1=["bird", "bird2", "bird3"]
elif whichlist==2:
list1=["bird", "bird2", "bird3"]
#more code
答案 0 :(得分:0)
在您的示例中,function
比class
更好。并且最好不要使用global
。{p>
在return
file2.py
在def get_list(whichlist)
if whichlist == 1:
return ["bird", "bird2", "bird3"]
elif whichlist == 2:
return ["bird", "bird2", "bird3"]
#else:
# return []
file1.py
如果您确实需要使用类,则应在使用from file2 import get_list
list1 = get_list(1)
对于类使用return
也是一个很好的规则-这意味着CamelCaseNames
而不是`example
在Example
file2.py
在class Example:
def get_list(self, whichlist):
if whichlist == 1:
return ["bird", "bird2", "bird3"]
elif whichlist == 2:
return ["bird", "bird2", "bird3"]
#else:
# return []
file1.py
最终
from file2 import Example
ex = Example()
list1 = ex.get_list(1)