Nathan Harmston a écrit : > HI, > > I m trying to start an api in a similar way to the djangic way of > Class.objects.all(). Ie objects is a "Manager" class. > > So: > > class Foo(object): > def __init__(self): > self.test = "NEE" > > class Manager(object): > def __init__(self): > pass > def all(self): > return "COCONUTS" > > Because of how some of the code is set up I cant use > metaclasses........so I try to use a decorator: > > def addto(instance): > def decorator(f): > import new > f = new.instancemethod(f, instance, instance.__class__) > setattr(instance, "objects", f) > return f > return decorator > > class Manager(object): > @addto(Foo) > def __init__(self): > ............. > > however this only binds the init method to the Foo.objects, so not > what I want.
Indeed. > If I try using classmethod...then it just says the > Foo.objects doesnt exist. You mean decorating Manager.__init__ with classmethod ??? I may be wrong, but I suspect you don't have a clear idea of what you're doing here. > Does anyone have any ideas how I can accomplish this using decorators? Yes : don't use a decorator !-) Instead of asking for how to implement what you think is the solution, you might be better explaining the problem you're trying to solve. > And also preventing more than one Manager instance instantiated at one > time. Q&D: class Singleton(object): def __new__(cls): if not hasattr(cls, '_inst'): cls._inst = object.__new__(cls) return cls._inst Same remark as above... -- http://mail.python.org/mailman/listinfo/python-list