eric wrote: > hi, > > I need to find a "good" design pattern to instanciate, and add > specific code all in one. Let me explain it : > > I need to define "some" code, better be in a class, something like > > class LinkA(object): > def mystuff(self): > <do something different> > > class LinkB(object): > def mystuff(self): > <do something different again> > > > AND I need an instance of this class > { "stuff A": LinkA() > "stuff B": LinkB() > } > > This kind of code "would" be fine, I mean, the result effect in memory > is fine for me. > But I don't like the way I have to > 1/ give a useless name to LinkA, linkB (there can be hundreds of names > like that) > 2/ I have to write it down two times (and that's one time too much) > > any ideas ? > > something like > [ > new object(): > def mystuff(self): > <do something> > , > new object(): > def mystuff(self): > <do something else> > ] > > would be really perfect (but I know it does not work, or at least, I > don't know how to make it work) > > In fact, I would like to define a class, and an instance in a single > statement
>>> class Register: ... def __init__(self): ... self.items = [] ... def __call__(self, method): ... class Link(object): ... mystuff = method ... self.items.append(Link()) ... >>> register = Register() >>> @register ... def mystuff(self): print "first" ... >>> @register ... def mystuff(self): print "second" ... >>> for item in register.items: ... item.mystuff() ... first second Peter -- http://mail.python.org/mailman/listinfo/python-list