Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
2 Risposte
5611 Visualizzazioni

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?

Avatar
Abbandona
Risposta migliore

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

Avatar
Abbandona
Risposta migliore

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

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
3
dic 17
11946
1
lug 25
2004
1
feb 25
1286
0
gen 24
4664
2
gen 19
22110