だから、私はPython辞書を追加して更新しています。現在はかなり醜いように見え、読みにくいですが、同じことをやっているより良い方法がありますか?python辞書の値を更新する
if not transaction_id in self.transaction_log:
self.transaction_log[transaction_id] = {
'gross_total': 0,
'net_total': 0,
'qty_total': 0,
'tax_total': 0
}
self.transaction_log[transaction_id]['products'] = {}
# create a list of dics to be reused in
# other class methods
self.transaction_log[transaction_id].update({
'transaction_id': transaction_id,
'transaction_time': transaction_datetime,
'location_id': location_id,
'till_id': till_id,
'employee_id': employee_id,
})
self.transaction_log[transaction_id]['products'][product_id] = {
'gross': gross,
'net': net,
'tax': tax,
'qty': qty
}
self.transaction_log[transaction_id]['gross_total'] += gross
self.transaction_log[transaction_id]['net_total'] += net
self.transaction_log[transaction_id]['qty_total'] += tax
self.transaction_log[transaction_id]['tax_total'] += qty
tax
は、我々は、このコードのためにもう少しコンテキストを持ってもらえ逆転のように見えますか?すでに動作しているコードをリファクタリングするだけで、通常はhttp://codereview.stackexchange.comで処理されます –私は、スニペットの先頭にある 'self.transaction_log [transaction_id]'への参照を参照することをお勧めしますそれをローカル変数として設定し、あなたは 'self.transaction_log [transaction_id] ['products'] = {}'を設定しますが、ちょうど上記の定義に 'products ':{}'を追加することができます。 –