On 7 dec 2005, at 06.21, Adrian Holovaty wrote:

But my rationale for changing to the magic object approach is: If we
move to class-based lookups (Person.objects.get_list() rather than
people.get_list()), the class will have been imported *anyway*, so
it's not too much of a big deal to have to use it.

The other magic-thread was too hot for me, I'll jump in here instead...

I find the use of "objects" as a way of namespace separation a bit off-putting. Enforcing an invocation-style via stack inspection would cut down on the line-noise. Not a terribly big deal, just how I'd try to do it unless there'd be something that precludes it...

import inspect

class TestClass:
    @classmethod
    def get_list(*args):
        frame = inspect.currentframe()
        frame = frame.f_back
        if not frame.f_locals:
            # called via Class
            print 'class'
        else:
# called via an instance, raise InvalidInvocationError or something...
            print 'instance'
            raise Exception

def main():
    TestClass.get_list('test')
    test = TestClass()
    test.get_list('test')

if __name__ == '__main__': main()


regards,
- Daniel

Reply via email to