【问题标题】:tuple error on writing properties of a class写入类的属性时出现元组错误
【发布时间】:2011-08-19 15:23:05
【问题描述】:

由于我是 stackoverflow 社区的新手,我不知道我是否应该将其作为一个新问题或我在这里提出的问题的延续 error with appending to a file and using an array 发布。

这里的代码基本相同,只是多了一行:

    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'

类组件明明有一个叫width的属性,所以不明白为什么会出现这个错误。 我知道该组件是一个组件数组,所以这可能是原因......但我试图在(组件)范围内使用 for ,但显然我缺乏使其工作的技能。 提前致谢。

【问题讨论】:

    标签: python tuples


    【解决方案1】:

    你的问题是这样的:

    Component_list = [Component1, Component2]                      
    system1 = system(Component_list)
    

    结合这个:

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

    要么去掉__init__中的星星,要么做

    system1 = system(Component1, Component2)
    

    没有先将它们分组到一个列表中。

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

    def __init__(self, *component, **kwargs):
        self.name = kwargs.get('name')
        self.component = component
        self.lines = kwargs.get('lines', [])
    

    如果您将这些components 放入列表中,则没有* 或相同。

    然后,而不是

    def writeTOFile(self,*component):
    
                    self.component = component
    

    只是做

    def writeTOFile(self):
    

    因为您已经在__init__ 中将component 添加到self

    然后,而不是

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

                for component in components:
                    line = "Width = %d" % component.width
                    self.lines.append(line)
    

    一切都应该正常。

    【讨论】:

    • 不完全是。他的__init__ 被严重擦伤。如果他使用**kwargs,他不需要命名参数namelines
    • 啊,我没有注意到他将它们从我在上一个答案中所做的更改中移回。谢谢。
    • 好的,伙计们!谢谢!我想我对几件事感到非常困惑。
    • 好的,谢谢!我不想撤消您对我的 init 所做的更改。事实上,我以为您给了我一个快速的答案并且也没有包括我的台词.. 所以这就是为什么我又加了一遍lol。在此之前我真的不熟悉kwargs或*。
    【解决方案2】:

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

    for x in component:
                for c in x:
                   line = "width = %d" % c.width
                   self.lines.append(line)
    

    【讨论】:

      猜你喜欢
      • 2020-01-30
      • 2017-11-27
      • 2021-03-20
      • 2020-04-05
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 2022-07-01
      相关资源
      最近更新 更多