class Foo: def __getattr__(self, key): print 'getting %s' % key raise KeyError, key foo = Foo() if not foo: print 'no foo'