import os, re
if __name__ == '__main__':
pass
else
from google.appengine.ext import webapp
register = webapp.template.create_template_register()
This works great, except my code makes use of the resister object in
several places, like this:
register.filter(emptylines)
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!
> 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
here is a class that accepts any method call without generating an error:
class Stub(object):
@staticmethod
def stub(*arg, **kwarg):
pass
def __getattribute__(self, name):
return Stub.stub
s = Stub()
s.foo('bar')
s.bar
s.bar('', '', 5)
JM
JM emailed me a good solution, but yours is great! Thanks!
JM
I have to apologize for not replying as soon as I got your email. It
did everything I needed, so I implemented it in my code and went to
town. Then, when I did finally return to the c.l.py, there was an
solution that exceeded my needs by letting me chain together arbitrary
lists of attributes. Now that I've slept on it, I've come up with a
solution that I like even more:
>>> class Placeholder(object):
def __getattr__(self, name):
return self
def __getitem__(self, index):
return self
def __call__(self, *args, **kwargs):
return self
>>> x = Placeholder()
>>> x('hello, world').y[42].z
<__main__.Placeholder object at 0x01E46490>
Yes, running it from the prompt looks ugly, but within a program the
return value is silently discarded.