[RFC] use optparse for option handling

Ben Finney bignose+hates-spam at benfinney.id.au
Fri Jul 14 00:30:10 BST 2006

Aaron Bentley <aaron.bentley at utoronto.ca> writes:

> Ben Finney wrote:
> > If you really want to check *identity*, use 'is' and 'is
> > not'.
> I really want to check identity.  I want to be able to use
> OptionParser.DEFAULT_VALUE as a special object, the same way None is
> used.

If that's truly the best way to implement the functionality, then
you'll need an object that *does* behave like None. Search for the
Singleton pattern.

However, that's a bad design smell, and doesn't quite seem to address
what you're actually looking for.

> I want the test to evaluate to True if v is another object with the
> same value as OptionParser.DEFAULT_VALUE.  It should only evaluate
> to False if v and OptionParser.DEFAULT_VALUE are two names for the
> same object.

Why not put that functionality in the object that holds that value,
and make it cognizant of whether the value is changed?

    class Foo(object):
        """ Hold a value and be aware of whether it's been changed. """

        def __init__(self):
            self._bar = "Bar default"
            self._bar_modified = False

        def _get_bar(self):
            return self._bar

        def _set_bar(self, value):
            self._bar_modified = True
            self._bar = value

        bar = property(_get_bar, _set_bar)

        def is_bar_modified(self):
            return self._bar_modified

    >>> this_foo = Foo()
    >>> this_foo.is_bar_modified()
    >>> this_foo.bar = "Ethel"
    >>> this_foo.is_bar_modified()
    >>> this_foo.bar = "Bar default"
    >>> this_foo.is_bar_modified()

 \      "People always ask me, 'Where were you when Kennedy was shot?' |
  `\                     Well, I don't have an alibi."  -- Emo Philips |
_o__)                                                                  |
Ben Finney

More information about the bazaar mailing list