附加到文件并使用数组时出错

发布于 2024-11-30 06:29:59 字数 1741 浏览 0 评论 0原文

我写了这段代码:

    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) ,其中 component = [Component1, Component2, ...Componentn] ?

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾冷 2024-12-07 06:29:59

您的 __init__ 中出现了一些乱七八糟的情况:

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

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

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

会起作用。您需要将 linesname 放在收集组件的 * 项之后。

在 Python 2 中,您不能在 * 之后使用命名属性,因此您需要使用 **kwargsget('name')< /code> 和 get('lines') 来自 kwargs

如果您不提供默认值,get 仅返回 None,因此您将在此处获得 self.name = None。如果你想指定一个默认名称,你可以

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

像我对lines所做的那样。

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.

阿楠 2024-12-07 06:29:59

在第 32 行中,您使用 self.lines.append(line) 。

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

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.

寂寞清仓 2024-12-07 06:29:59

问题在于,在定义 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 个组件的系统,并使用一个空列表作为线路的“控制台”)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文