'Get an item or attribute of an object but prefer the item.'

def getitem(self, obj, argument):

    try:
        return obj[argument]
    except (TypeError, LookupError):
        if isinstance(argument, basestring):
            try:
                attr = str(argument)
            except:
                pass
            else:
                try:
                    return getattr(obj, attr)
                except AttributeError:
                    pass
        return self.undefined(obj=obj, name=argument)
