Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Why context=None ?

By
Vincent
on 4/13/15, 11:20 AM 894 views

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?

0
NUMERIGRAPHE
On 6/16/15, 3:40 AM

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

-1
Ludovic CHEVALIER
On 4/14/15, 2:58 AM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 4/13/15, 11:20 AM
Seen: 894 times
Last updated: 6/16/15, 3:40 AM