
Reviewed-by: Rodrigo Trujillo <rodrigo.trujillo@linux.vnet.ibm.com> On 06/07/2016 01:29 AM, Ramon Medeiros wrote:
For mutable object as a default parameter in function and method-declarations the problem is, that the evaluation and creation takes place at exactly the same moment. The python-parser reads the function-head and evaluates it at the same moment.
Most beginers assume that a new object is created at every call, but that's not correct. One object (in your example a list) is created at the moment of declaration and not on demand when you are calling the method.
For imutable objects that's not a problem, because even if all calls share the same object, it's imutable and therefore it's properties remain the same.
As a convention, must be used the None object for defaults to indicate the use of a default initialization, which now can take place in the function-body, which naturally is evaluated at call-time.
Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- src/wok/exception.py | 4 +++- src/wok/message.py | 4 +++- src/wok/model/notifications.py | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/src/wok/exception.py b/src/wok/exception.py index 52f007e..90b6410 100644 --- a/src/wok/exception.py +++ b/src/wok/exception.py @@ -25,7 +25,9 @@ from wok.message import WokMessage
class WokException(Exception): - def __init__(self, code='', args={}): + def __init__(self, code='', args=None): + if args is None: + args = {} self.code = code msg = WokMessage(code, args).get_text() cherrypy.log.error_log.error(msg) diff --git a/src/wok/message.py b/src/wok/message.py index a7162c7..4c35747 100644 --- a/src/wok/message.py +++ b/src/wok/message.py @@ -26,7 +26,9 @@ from wok.template import get_lang, validate_language
class WokMessage(object): - def __init__(self, code='', args={}, plugin=None): + def __init__(self, code='', args=None, plugin=None): + if args is None: + args = {} # make all args unicode for key, value in args.iteritems(): if isinstance(value, unicode): diff --git a/src/wok/model/notifications.py b/src/wok/model/notifications.py index e83ac56..bdb7c78 100644 --- a/src/wok/model/notifications.py +++ b/src/wok/model/notifications.py @@ -27,7 +27,9 @@ from wok.utils import wok_log notificationsStore = {}
-def add_notification(code, args={}, plugin_name=None): +def add_notification(code, args=None, plugin_name=None): + if args is None: + args = {} if not code: wok_log.error("Unable to add notification: invalid code '%(code)s'" % {'code': str(code)})