首页 > 解决方案 > 这行带有 [0][1] 索引的代码有什么用?

问题描述

我不知道 name_get()[0][1] 是什么

display_name = product_id.name_get()[0][1]
       if product_id.description_sale:
           display_name += '\n' + product_id.description_sale

标签: python-3.7odoo-12

解决方案


向后分解:[0][1]表示二维数组的一个元素(一个数组的元素也是数组),因此我们可以推断期望name_get()返回一个二维数组——我们不能说任何关于值的类型虽然在这些数组中 - python 是动态类型的。

product.name_get()表示 name_get() 是产品类/文件的方法/函数。

例如 -name_get()可能会返回类似

[ ["savings account", "current account"], ["credit card", "store card"] ]

所以name_get()[0][1]会评估为"current account"


推荐阅读