error with appending to a file and using an array

2019-08-13 05:00发布

I have written this code:

    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)

and I get the error :

  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'

And I don't really know how to fix it.

Also is there a way for defining my system1 as system(Component) where component = [Component1, Component2, ...Componentn] ?

Thanks in adavance

3条回答
啃猪蹄的小仙女
2楼-- · 2019-08-13 05:26

The problem is in the fact that when defining system you pass Component1 as a line argument in constructor. Since python does all the operations he can and not checking for the argument types if the operation can be done legally, this passes.

Maybe it would be a nice idea in the system constructor to check if the given argument lines is really of type list, and maybe writing something like:

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

That way, you would know about the problem before you try appending to the non-list object.

And as for the second part of your question, you can do it exactly like you suggested:

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

(if you, for example, want to make a system with 3 components, and an empty list as an "console" for lines)

查看更多
仙女界的扛把子
3楼-- · 2019-08-13 05:28

in line 32 you use self.lines.append(line).

But lines is a member of the class system initialized with Component2, which type is the class component that does not have the method append.

查看更多
太酷不给撩
4楼-- · 2019-08-13 05:32

You've got things out of order in your __init__:

  def __init__(self, *component, **kwargs):

    self.name = kwargs.get('name')
    self.component = component

    self.lines = kwargs.get('lines', [])

Will work. You need lines and name to be after the * item that collects the component.

In Python 2, you can't then have named attributes after a *, so you need to instead use **kwargs and get('name') and get('lines') from the kwargs.

get just returns None if you don't supply a default, so you'll get self.name = None here. If you want to specify a default name, you can do

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

like I did for lines.

查看更多
登录 后发表回答