diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2016-12-13 11:53:47 -0800 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2016-12-13 12:34:15 -0800 |
commit | 02431efd92060ea5d1f64bfc700ae9b1e5f4907d (patch) | |
tree | 5fc98c8c4011bc84c2c87cec26eafd5f3bfbfefe /alot/ui.py | |
parent | 42504da6791fb12527c708759715b7a79d5a1ddb (diff) |
Replace mutable keyword arguments
There are a number of cases of mutable keyword arguments (list and dict
in this case). Mutable keyword arguments are rather dangerous, since any
mutation of the default value is persistent, which will inevitably lead
to bugs.
For example, imagine this code:
def func(def=[]):
def.append('foo')
return def
>>> func()
['foo']
>>> func()
['foo', 'foo']
This is almost certainly not what was intended. This code generally uses
the idiom of setting the default value to None, and then assigning with
or `value = value or []` which will replace value with the empty list
(or dict) when value is falsey, like None or another empty list.
Diffstat (limited to 'alot/ui.py')
-rw-r--r-- | alot/ui.py | 9 |
1 files changed, 6 insertions, 3 deletions
@@ -241,7 +241,7 @@ class UI(object): self._unlock_callback = afterwards self._locked = True - def prompt(self, prefix, text=u'', completer=None, tab=0, history=[]): + def prompt(self, prefix, text=u'', completer=None, tab=0, history=None): """ prompt for text input. This returns a :class:`~twisted.defer.Deferred` that calls back with @@ -260,6 +260,8 @@ class UI(object): :type history: list of str :rtype: :class:`twisted.defer.Deferred` """ + history = history or [] + d = defer.Deferred() # create return deferred oldroot = self.mainloop.widget @@ -442,8 +444,8 @@ class UI(object): self._notificationbar = None self.update() - def choice(self, message, choices={'y': 'yes', 'n': 'no'}, - select=None, cancel=None, msg_position='above'): + def choice(self, message, choices=None, select=None, cancel=None, + msg_position='above'): """ prompt user to make a choice. @@ -462,6 +464,7 @@ class UI(object): :type msg_position: str :rtype: :class:`twisted.defer.Deferred` """ + choices = choices or {'y': 'yes', 'n': 'no'} assert select in choices.values() + [None] assert cancel in choices.values() + [None] assert msg_position in ['left', 'above'] |