Fredrik Lundh wrote:
Steven Bethard wrote:

I've seen at least one reasonable example of this kind of thing:

http://mail.python.org/pipermail/python-list/2004-October/245432.html

the code he's referring to doesn't seem to use that construct anymore, so it's not obvious what "dejavu.icontains" etc really is, but assuming they're strings, the following is a reasonable lambda-free alternative:

class xforms:
    def icontains(self, x, y): return x + " Like '%" + y[1:-1] + "%'"
    def icontainedby(self, x, y):
        # icontainedby body goes here instead of in a separate function
    def istartswith(self, x, y): return x + " Like '" + y[1:-1] + "%'"
    def iendswith(self, x, y): return x + " Like '%" + y[1:-1] + "'"
    def ieq(self, x, y): return x + " = " + y
    def now(self): return "Now()"
    def today(self): return "DateValue(Now())",
    def year(self, x): return "Year(" + x + ")"
    def dispatch(self, tag, *args):
        return getattr(self, tag)(*args)

This doesn't work if the strings aren't valid Python identifiers (e.g. "!CONTAINS!"). On the other hand, if they are (and you can be guaranteed that they will continue to be), then your solution above is nice, though if you want to fully reproduce the behavior, you probably also want the method


def __getitem__(self, item):
    return getattr(self, item)

Steve
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to