Python 写入类的属性时出现元组错误

Python 写入类的属性时出现元组错误,python,tuples,Python,Tuples,由于我是stackoverflow社区的新成员,我不知道是否应该将此作为一个新问题或是我在这里提出的问题的延续发布 这基本上是相同的代码,只是增加了一行: class component(object): def __init__(self, name = None, height = None, width = None):

由于我是stackoverflow社区的新成员,我不知道是否应该将此作为一个新问题或是我在这里提出的问题的延续发布

这基本上是相同的代码,只是增加了一行:

    class component(object):

      def __init__(self,
                   name = None,
                   height = None,                 
                   width = None):

        self.name = name        
        self.height = height
        self.width = width


class system(object):

      def __init__(self,
                   name = None,                 
                   lines = None,
                   *component,
                   **kwargs):

                    self.name = kwargs.get('name')
                    self.component = component
                    self.lines = kwargs.get('lines') or []


      def writeTOFile(self,*component):

                    self.component = component

                    line =" "
                    self.lines.append(line)

                    line= "#----------------------------------------- SYSTEM ---------------------------------------#" 
                    self.lines.append(line)


                    line = "Width = %d" % component.width
                    self.lines.append(line)


      def writeFile(self):

                    ef = open('file1.d', 'w')
                    ef.write('\n'.join(self.lines))
                    ef.close()

Component1 = component ( name = 'C1',
                         height = 500,
                         width = 400)
Component2 = component ( name = 'C2',
                         height = 600,
                         width = 700)
Component_list = [Component1, Component2]                      
system1 = system(Component_list)
system1.writeTOFile(Component_list)
system1.writeFile()
我添加的行是:

line = "Width = %d" % component.width
self.lines.append(line)
我得到的错误是:

Traceback (most recent call last):
  File "C:\Python27\Work\trial2.py", line 55, in <module>
    system1.writeTOFile(Component_list)
  File "C:\Python27\Work\trial2.py", line 37, in writeTOFile
    line = "Width = %d" % component.width
AttributeError: 'tuple' object has no attribute 'width'
回溯(最近一次呼叫最后一次):
文件“C:\Python27\Work\trial2.py”,第55行,在
system1.writeTOFile(组件列表)
文件“C:\Python27\Work\trial2.py”,第37行,以writeTOFile格式
line=“Width=%d”%component.Width
AttributeError:“tuple”对象没有属性“width”
类组件显然有一个名为width的属性,所以我不明白为什么会出现这个错误。 我知道组件是一个组件数组,所以这可能是。。。但我尝试使用(组件)范围内的for,但显然我缺乏使其工作的技能。
提前感谢。

参数*组件是一个元组。此外,您正在向writeToFile传递一个组件列表,而在方法中,您正在使用传递的参数作为组件

for x in component:
            for c in x:
               line = "width = %d" % c.width
               self.lines.append(line)
你的问题是:

Component_list = [Component1, Component2]                      
system1 = system(Component_list)
与此相结合:

  def __init__(self,
               name = None,                 
               lines = None,
               *component,
               **kwargs):
\uuuu init\uuuu
中去掉星号,或者

system1 = system(Component1, Component2)
没有先将它们分组到列表中

另外,您的
\uuuu init\uuuu
又错了,您已经撤消了我在回答上一个问题时建议的更改。您需要它看起来像:

def __init__(self, *component, **kwargs):
    self.name = kwargs.get('name')
    self.component = component
    self.lines = kwargs.get('lines', [])
如果您将这些
组件
放入列表中,则不使用
*
也可以使用相同的方法

然后,而不是

def writeTOFile(self,*component):

                self.component = component
                line = "Width = %d" % component.width
                self.lines.append(line)
照办

def writeTOFile(self):
因为您已经将
组件
添加到
中的
self

然后,而不是

def writeTOFile(self,*component):

                self.component = component
                line = "Width = %d" % component.width
                self.lines.append(line)


一切都应该正常。

不完全是这样。他的
\uuuu init\uuuu
严重擦伤。如果他使用**kwargs,他不需要命名参数
name
啊,我没有注意到他将它们从我上次回答中所做的更改中移回。谢谢,好的,伙计们!谢谢我想有几件事让我很困惑。好的,谢谢!我不想撤消您对我的init所做的更改。事实上,我以为您给了我一个快速的答案,并且没有包括我的行。。这就是为什么我再次添加它们的原因。在这之前,我对kwargs或*真的不熟悉。