python - 当函数看起来为空时,在函数中使用列表变量
问题描述
我正在创建一个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()
解决方案
好的,我通过电子邮件获得了一些帮助。
Python 对象赋值很有趣。据我了解,当lst
(对象)被分配给另一个变量(new_lst
也成为对象变量)时,一个对象变量发生的事情会影响另一个
>>> 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']
推荐阅读
- vue.js - vuelidate 验证对象作为道具传递
- jmeter - 以非 gui 模式运行 jmeter,为 groovy 脚本发送参数,如何?
- xml - 使用 XDocument 解析 XML - vb.net
- here-api - Here Fleet Telematics Toll Costs Api 中用于成本计算的车辆重量参数中可能存在错误
- html - 在 CSS 网格中定位框项目的问题
- r - geom_candlestick 没有在自己的数据上按应有的方式绘制编辑:将 POSIXct 转换为日期
- css - 电子邮件的动态高度 100% td
- c# - 如何在运行时将安全协议更新为 TLS 1.2
- c# - 如何使用标记属性而不是标记接口?
- python - 使用 Python:分组和绘图比率以比较它们,添加额外的计算(即:直方图、散点图、密度图)