Python错误:AttributeError:type object' MyClass'没有属性'频道'

时间:2017-09-29 00:25:24

标签: python python-2.7

我的代码需要一些帮助。我在输入self.channel时在player.py中获取import test列表时遇到问题,但我收到错误:AttributeError:type object' MyClass'没有属性'频道'当我尝试运行脚本时。

此行突出显示错误:

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脚本中获取列表吗?

1 个答案:

答案 0 :(得分:5)

channel()删除括号。这是一个领域,而不是一个功能。将括号添加到MyClass,以便调用构造函数。

self.channel = test.MyClass().channel

所有代码放在一起:

test.py

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

player.py

import test

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

    def test(self):
        return self.channel


print MyPlayer().test()