首页 > 解决方案 > 当函数看起来为空时,在函数中使用列表变量

问题描述

我正在创建一个Frappe App,存在类似(工作)功能,所以我试图重用此代码。我在这个 python 文档中的任何地方都找不到lst变量的实际赋值(使用搜索,它是这个函数的局部变量)。到目前为止,它在声明时似乎是一个空列表,但是,它被视为包含一个值。

还是我只是误解了代码本身?

首先,lst被声明为一个列表(空)

lst = []

它被分配给 reconciled_entry (仍然为空)

reconciled_entry = lst

然后按如下方式使用:(还是空的)

if lst:
        reconcile_against_document(lst)

函数 reconcile_against_document 函数需要一个实际列表:

还是空的???

def reconcile_against_document(args):
    """
        Cancel JV, Update against document, split if required and resubmit jv
    """
    for d in args:

        check_if_advance_entry_modified(d)
        validate_allocated_amount(d)

        # cancel advance entry
        doc = frappe.get_doc(d.voucher_type, d.voucher_no)

        doc.make_gl_entries(cancel=1, adv_adj=1)
...

主功能:

def reconcile(self, args):
        for e in self.get('payments'):
            e.invoice_type = None
            if e.invoice_number and " | " in e.invoice_number:
                e.invoice_type, e.invoice_number = e.invoice_number.split(" | ")

        self.get_invoice_entries()
        self.validate_invoice()
        dr_or_cr = ("credit_in_account_currency"
            if erpnext.get_party_account_type(self.party_type) == 'Receivable' else "debit_in_account_currency")

        lst = []
        dr_or_cr_notes = []
        for e in self.get('payments'):
            reconciled_entry = []
            if e.invoice_number and e.allocated_amount:
                if e.reference_type in ['Sales Invoice', 'Purchase Invoice']:
                    reconciled_entry = dr_or_cr_notes
                else:
                    reconciled_entry = lst

                reconciled_entry.append(self.get_payment_details(e, dr_or_cr))

        if lst:
            reconcile_against_document(lst)

        if dr_or_cr_notes:
            reconcile_dr_cr_note(dr_or_cr_notes)

        msgprint(_("Successfully Reconciled"))
        self.get_unreconciled_entries()

标签: pythonfrappe

解决方案


好的,我通过电子邮件获得了一些帮助。

Python 对象赋值很有趣。据我了解,当lst(对象)被分配给另一个变量(new_lst也成为对象变量)时,一个对象变量发生的事情会影响另一个

python演示截图

>>> lst = []
>>> new_lst = lst
>>> new_lst.append('funny python')
>>> lst
['funny python']
>>> lst.append('not funny python')
>>> new_lst
['funny python', 'not funny python']
>>> lst
['funny python', 'not funny python']

推荐阅读