【问题标题】:error with appending to a file and using an array附加到文件并使用数组时出错
【发布时间】:2011-10-29 19:38:56
【问题描述】:

我已经写了这段代码:

    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):

        self.name = name
        self.component = component

        if lines is None:
                self.lines = []
        else:
                            self.lines = lines

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

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

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


Component1 = component ( name = 'C1',
                         height = 500,
                         width = 400)
Component2 = component ( name = 'C2',
                         height = 600,
                         width = 700)

system1 = system(Component1, Component2)
system1.writeTOFile(Component1, Component2)

我得到了错误:

  Traceback (most recent call last):
  File "C:\Python27\Work\trial2.py", line 46, in <module>
    system1.writeTOFile(Component1, Component2)
  File "C:\Python27\Work\trial2.py", line 32, in writeTOFile
    self.lines.append(line)
AttributeError: 'component' object has no attribute 'append'

而且我真的不知道如何解决它。

还有一种方法可以将我的 system1 定义为 system(Component) where component = [Component1, Component2, ...Componentn] ?

提前感谢

【问题讨论】:

    标签: python arrays append


    【解决方案1】:

    您的__init__ 出现问题:

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

    会工作。您需要linesname 位于收集组件的* 项之后。

    在 Python 2 中,您不能在 * 之后命名属性,因此您需要使用 kwargs 中的 **kwargsget('name')get('lines')

    get 只返回None 如果你不提供默认值,所以你会在这里得到self.name = None。如果你想指定一个默认名称,你可以这样做

        self.name = kwargs.get('name', 'defaultname')
    

    就像我为 lines 所做的那样。

    【讨论】:

      【解决方案2】:

      问题在于,在定义system 时,您将Component1 作为line 参数传递给构造函数。由于 python 做了他能做的所有操作,并且如果操作可以合法地完成,则不检查参数类型,这通过了。

      也许在系统构造函数中检查给定参数 lines 是否真的是列表类型是一个好主意,并且可能写如下内容:

          if lines is None or not isinstance(lines, list):
                  self.lines = []
          else:
                  self.lines = lines
      

      这样,您在尝试追加到非列表对象之前就会知道问题。

      至于你问题的第二部分,你可以完全按照你的建议去做:

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

      (例如,如果您想创建一个包含 3 个组件的系统,并将一个空列表作为行的“控制台”)

      【讨论】:

      • Python 完全有能力自动聚合参数列表,没有理由传递空列表。
      【解决方案3】:

      在第 32 行你使用self.lines.append(line)

      但是lines是Component2初始化的类system的成员,该类型是没有append方法的类component

      【讨论】:

      • 这并没有告诉他如何解决问题。
      • 你是对的,阅读你的回答我注意到他真正的问题是订单,而Component2 不应该是lines。我的回答很没用,对不起
      • 其实我没有理解错误,因为我认为 append 就像 python 中的一个通用函数......所以我不需要在我定义的任何类中定义它。
      猜你喜欢
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 2020-08-24
      相关资源
      最近更新 更多