跳至內容
選單
此問題已被標幟
2 回覆
5585 瀏覽次數

My question is bit pythonic. but I am curious to know the answer.

I have seen in method Odoo is passing context=None. like

def create(self, cr, uid, vals, context=None):
     if not context: context = {}
     '''
     code
     '''

Why they have not used context={}? like

def create(self, cr, uid, vals, context={}):
     '''
code
     '''

can anyone shade light on it?

頭像
捨棄
最佳答案

Because {} is mutable and None is immutable.

Mutable default values are awkward: if you change the value in you function it will also change the *default* value for all later calls of your function.

See https://doc.odoo.com/contribute/15_guidelines/coding_guidelines_framework/#the-infamous-context

The lengthy discussion about that here: https://bugs.launchpad.net/openobject-addons/+bug/525808

頭像
捨棄
最佳答案

This notation in python mean that context is a optionnal arg of the function. If no context is given, context is equal to none.

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
3
12月 17
11928
1
7月 25
1988
1
2月 25
1270
0
1月 24
4658
2
1月 19
22094