Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
5604 มุมมอง

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.

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
3
ธ.ค. 17
11938
1
ก.ค. 25
1993
1
ก.พ. 25
1281
0
ม.ค. 24
4661
Pass context in calling of super method. แก้ไขแล้ว
2
ม.ค. 19
22096