simple (I hope!) problem
samwyse
samwyse at gmail.com
Thu Aug 5 01:43:25 EDT 2010
On Aug 3, 1:20 am, Steven D'Aprano <steve-REMOVE-
T... at cybersource.com.au> wrote:
> On Mon, 02 Aug 2010 17:19:46 -0700, samwyse wrote:
> > Fortunately, I don't need the functionality of the object, I just want
> > something that won't generate an error when I use it. So, what is the
> > quickest way to to create such an object (replacing the 'pass' in my
> > first snippet). My solution is this:
>
> > class C:
> > def filter(self, *args, **kwds):
> > pass
> > register = C()
>
> > but it seems like I should be able to do something "better", as measured
> > by lines of code, faking more than just a 'filter' method, or both. Any
> > ideas? Thanks!
>
> You want a variation on the Null Object design pattern.
>
> class NullWithMethods(object):
> def __getattr__(self, name):
> return self
> def __call__(self, *args, **kwargs):
> pass
>
> And in action:
>
> >>> c = NullWithMethods()
> >>> c.spam("hello", "world")
> >>> c.something_completely_unlikely.spam.ham("hello", "world", foo=42)
>
> --
> Steven
JM emailed me a good solution, but yours is great! Thanks!
More information about the Python-list
mailing list