This question has been flagged
1 Reply
3478 Views

Hi,

I have searched through several sites, but I don't find how this "programming technique" is called.
To give an example: Inside the purchase module there is a function for counting all shipments for a purchase:

def _count_all(self, cr, uid, ids, field_name, arg, context=None):
        return {
            purchase.id: {
                'shipment_count': len(purchase.picking_ids),
                'invoice_count': len(purchase.invoice_ids),                
            }
            for purchase in self.browse(cr, uid, ids, context=context)
        }

I don't get why it is possible to put a for loop inside in a dictionary, and then, putting it even _AFTER_ the dictionary key purchase.id. Is there a name for this kind of programing technique so I can google it up? Or can anybode explain me, why it works to put it after purchase.id? Are dictionaries parsed by from the buttom to the top? Thanks for help!

Avatar
Discard
Best Answer

Hi,

Your code is executed as like below notmal code.

res= {}

for purchase in self.browse(cr, uid, ids, context=context):

    res.update({purchase.id:{
                                             'shipment_count': len(purchase.picking_ids),
                                             'invoice_count': len(purchase.invoice_ids),  }

                                         })

return res

In your code all this things are written into {} (Dictionary) thats why looping is manage by {} (Dict) it self. We can also write loop inside of list objcet like res = [purchase.name for purchase in self.browse(cr, uid, ids, context=context) ]. So, here res is contain the list of name from all purchase order.

I hope it is useful to you.

Avatar
Discard