Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
5610 Widoki

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?

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
3
gru 17
11945
1
lip 25
2002
1
lut 25
1285
0
sty 24
4664
2
sty 19
22106