Python 附加来自另一个类的列表

Python 附加来自另一个类的列表,python,python-3.x,Python,Python 3.x,我正在尝试将元组列表从合并到不同的类 我有一个类解析器,元组将在其中合并。类和相应的函数定义为: class parser(): def __init__(self): self.booklist = [] def parsing_write(self, filename): # print(self.booklist) datalist = [] writer = BibTexWriter() wr

我正在尝试将元组列表从合并到不同的类

我有一个类解析器,元组将在其中合并。类和相应的函数定义为:

class parser():
    def __init__(self):
        self.booklist = []
    def parsing_write(self, filename):
        # print(self.booklist)
        datalist = []
        writer = BibTexWriter()
        writer.indent = '    '
        print(self.booklist)
        for ref in self.booklist:
            print(type(ref))
            datadict = dict((k, v) for k, v in
                            zip(self.entries, ref) if v is not None)
            datalist.append(datadict)
在另一个函数中,我完成了:

import pybib
class Window(Gtk.ApplicationWindow):
    def __init__(self, application, giofile=None):
        self.Parser = pybib.parser()

    def get_data(self, datalist):
        datatup = tuple([name] + [self.KeyEntry.get_text()] +
                        [self.all_fields[field].get_text() or None
                         for field in fields])
        print(type(datatup))
        print(type(self.Parser.booklist))
        self.Parser.booklist.append(datatup)
        print((self.Parser.booklist))
我希望
Class窗口中的
datatup
将附加到
类解析器中的
图书列表中。但事实并非如此。
因此,为了显示,
parsing\u write
print(self.booklist)
是:

然后,如果我调用
get_data
,我将得到以下结果:

<class 'tuple'>
<class 'list'>
[('Article', 'key', None, 'au', None, None, None, None, None)]

[('Article','key',None',au',None,None,None,None,None,None]
但是我希望这个列表将附加到
解析\u write


N.B.请忽略每个列表的长度,因为我已经删除了它们。

在您的窗口类中,您没有返回列表,那么您的解析器如何知道您正在尝试做什么?只需向self.datatup发出一个return语句,然后将该方法调用的结果传递给类解析器,它就可以工作了

import pybib
class Window(Gtk.ApplicationWindow):
    def __init__(self, application, giofile=None):
        self.Parser = pybib.parser()

    def get_data(self, datalist):
        self.datatup = tuple([name] + [self.KeyEntry.get_text()] +
                        [self.all_fields[field].get_text() or None
                         for field in fields])
        print(type(self.datatup))
        print(type(self.Parser.booklist))
        self.Parser.booklist.append(self.datatup)
        print((self.Parser.booklist))
        return(self.datatup)

在Window类中,您没有返回列表,因此解析器如何知道您要做什么?只需向self.datatup发出一个return语句,然后将该方法调用的结果传递给类解析器,它就可以工作了

import pybib
class Window(Gtk.ApplicationWindow):
    def __init__(self, application, giofile=None):
        self.Parser = pybib.parser()

    def get_data(self, datalist):
        self.datatup = tuple([name] + [self.KeyEntry.get_text()] +
                        [self.all_fields[field].get_text() or None
                         for field in fields])
        print(type(self.datatup))
        print(type(self.Parser.booklist))
        self.Parser.booklist.append(self.datatup)
        print((self.Parser.booklist))
        return(self.datatup)

我可以知道投票结果接近的原因吗?如果我知道错误,我可以改进这个问题。我可以知道投票结果接近的原因吗?如果我知道错误,我可以改进这个问题。如果我没有遗漏任何内容,我只需要在
get\u data
的末尾添加
return(self.datatup)
。我试过了,但它不起作用。你是否实例化了对象并调用了get方法,将其存储在一个变量中并传递到parser obj方法中?不……实际上,这就是我更新BookList所做的一切如果我没有遗漏任何内容,我只需要在
get\u data
的末尾添加
return(self.datatup)
。我试过了,但它不起作用。你是否实例化了对象并调用了get方法,将其存储在一个变量中并传递到parser obj方法中?不……实际上,这就是我更新图书列表所做的全部工作