adding a static class to another class

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Sep 18 10:22:12 CEST 2007


Nathan Harmston a écrit :
> 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()
> 

You're obviously overcomplexifying things here...

# the canonical pythonic way
class Foo(object):
   objects = Manager()

   def __init__(self):
     self.a = "NEE"


# the monkeypatch way
class Foo(object):
   def __init__(self):
     self.a = "NEE"

Foo.objects = Manager()



HTH



More information about the Python-list mailing list