adding a static class to another class

Nathan Harmston ratchetgrid at googlemail.com
Mon Sep 17 19:22:37 EDT 2007


Hi,

I guess my description was a bit rubbish in retrospec, I dont even
think the title of my email made sense....it doesnt to me now:

class Manager(object):
  def __init__(self):
    pass
  def dosomething(self):
    return "RESULTS"

class Foo(object):
  def __init__(self):
    self.a = "NEE"

What I m trying to do is end up with the following syntax:

f = Foo() # instantiates a Foo object
g= Foo.objects.dosomething() # returns "RESULTS"

The best way I ve found of doing this is overriding new

class Foo(object):
    def __new__(cls, *args, **kw):
      cls.objects = Manager()

----> If I do it like this, I get exactly what I want - especially if
I change the new method in Manager to one supplied by Bruno.

However, I m playing around with SQLAlchemy (which is going to be used
by some of the Managers I create, others will use different things)
which means that overriding new is not allowed, I cant assign a mapper
to Foo.

So I was trying to think of another way of doing cls.objects =
Manager()....and the only solution I could think of was to use a
decorator and try to do it somehow.......

>From Brunos answer I guess a decorator is not a good way of
accomplishing this, so if there anyway of doing this without using a
metaclass......or by using a decorator or not....any help would be
greatly appreciated.

Many Thanks in advance

Nathan









On 17/09/2007, Bruno Desthuilliers
<bdesth.quelquechose at free.quelquepart.fr> wrote:
> 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
>



More information about the Python-list mailing list