元气_hi 2022-06-04 20:08 采纳率: 80%
浏览 20
已结题

python:类列表加载错误

使用PYTHON3.8
期望:成功加载register.command

#tell.py
import bin.register

class tell:
    def __init__(self):

        bin.register.register.adder(tell) 
        #commands为[<class '__main__.tell'>]
 tell()
#register.py
class register:
    commands = []

    @staticmethod
    def getter():
        return register.commands #被tell调用时为[<class '__main__.tell'>]


    @staticmethod
    def adder(item):
        register.commands +=[item] #被tell调用时为[<class '__main__.tell'>]



    @staticmethod
    def setter(list1):
        register.commands = list1

# main.py
    from bin.register import register
    for i in os.listdir("D:/Flyer1.0/lib/command/"):                                        #
        if os.path.isfile("D:/Flyer1.0/lib/command/"+i) and i.endswith('.py'): #获得D:\Flyer1.0\lib\command\下所有的py文件并运行
            os.system(os.path.join("D:/Flyer1.0/lib/command/",i))                 #结果:tell.py 
    while True:
        for i in register.commands:                        # 在register中的command列表 **逻辑错误:command为[]**
            ans = input("/Flyer1.0/run>").split(' ',1)
            i.run_command(ans[1])

只有在main.py的第二个for循环中有问题(command为空)

  • 写回答

1条回答 默认 最新

  • 元气_hi 2022-06-05 12:53
    关注

    的了,我换种方法:使用一个import lib.commands.tell的文件加eval ok了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月13日
  • 已采纳回答 6月5日
  • 创建了问题 6月4日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效