[PATCH 1 of 2] ui: add optional choices parameter to prompt()

Martijn Pieters mj at zopatista.com
Tue Apr 28 09:34:59 CDT 2009


On Tue, Apr 28, 2009 at 06:11, Steve Borho <steve at borho.org> wrote:
> -    def prompt(self, msg, pat=None, default="y"):
> +    def prompt(self, msg, pat=None, default="y", choices={}):
>         """Prompt user with msg, read response, and ensure it matches pat

Never use a mutable (such as a dict) as a keyword argument default;
you'll end up changing it and then find that the default was altered
for all invocations:

>>> def demo(key, value, mapping={}):
...     mapping[key] = value
...     return mapping
...
>>> demo('foo', 'bar')
{'foo': 'bar'}
>>> demo('spam', 'eggs')
{'foo': 'bar', 'spam': 'eggs'}

Rather, use a non-mutable dongle and create the mutable in the function body:

>>> def betterdemo(key, value, mapping=None):
...     if mapping is None:
...         mapping = {}
...     mapping[key] = value
...     return mapping
...
>>> betterdemo('foo', 'bar')
{'foo': 'bar'}
>>> betterdemo('spam', 'eggs')
{'spam': 'eggs'}

-- 
Martijn Pieters



More information about the Mercurial-devel mailing list