Python错误:attribute error:类型对象“MyClass”没有属性“channel”

2024-04-19 18:40:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一些代码方面的帮助。当我有输入import test时,在player.py中获取self.channel的列表有问题,但是我得到了一个错误:attribute error:type object'MyClass'在尝试运行脚本时没有属性'channel'。

此行上的错误突出显示:

self.channel = test.MyClass.channel()

在test.py中显示:

from player import MyPlayer

class MyClass(xbmcgui.WindowXML):

def __init__(self, *args, **kwargs):
    self.channel = list()

在player.py中:

import test

class MyPlayer(xbmcgui.WindowXML):
  def __init__(self, *args, **kwargs):
      self.channel = test.MyClass.channel()

我想从test.py中获取self.channel,以获取字符串列表。你能告诉我如何让self.channel从test.py脚本获取列表吗?


Tags: pytestimportself脚本列表def错误
1条回答
网友
1楼 · 发布于 2024-04-19 18:40:13

channel()中删除括号。它是一个字段,而不是函数。在MyClass中添加括号,以便调用构造函数。

self.channel = test.MyClass().channel

所有代码组合在一起:

测试.py

class MyClass:
    def __init__(self, *args, **kwargs):
        self.channel = list()

玩家.py

import test

class MyPlayer:
    def __init__(self, *args, **kwargs):
        self.channel = test.MyClass().channel

    def test(self):
        return self.channel


print MyPlayer().test()

相关问题 更多 >