2011-08-19 54 views
1

因为我是新来的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,但显然我缺乏使其工作的技能。 在此先感谢。

回答

0

你的问题是这样的:

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', []) 

或不*相同的,如果你把这些component s转换列表中,你所做的一切。

然后,而不是

def writeTOFile(self,*component): 

       self.component = component 

只是做

def writeTOFile(self): 

,因为你已经在__init__添加componentself

然后,而不是

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

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

,一切都应该工作。

+0

不完全。他的'__init__'严重受伤。如果他使用** kwargs,他不需要命名参数'name'和'lines' –

+0

啊,我没有注意到他从我在最后一个答案中所做的更改中移除了他们。谢谢。 – agf

+0

好男人!谢谢!我认为我对几件事情感到非常困惑。 – caran

0

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

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