Re: decorator and API

2008-09-18 Thread Gerard flanagan
Lee Harr wrote: I have a class with certain methods from which I want to select one at random, with weighting. The way I have done it is this import random def weight(value): def set_weight(method): method.weight = value return method return set_weight class A(o

Re: decorator and API

2008-09-18 Thread Peter Otten
Steven D'Aprano wrote: I agree with you that the simple explicit approach is better. Now, to answer the question the OP didn't ask: > def choose_with_weighting(actions, weights=None): >     if weights is None: >         weights = [1]*len(actions)  # equal weights >     # Taken virtually unchanged

Re: decorator and API

2008-09-17 Thread George Sakkis
On Sep 17, 5:56 pm, Lee Harr <[EMAIL PROTECTED]> wrote: > I have a class with certain methods from which I want to select > one at random, with weighting. > > The way I have done it is this > > import random > > def weight(value): > def set_weight(method): > method.weight = value

Re: decorator and API

2008-09-17 Thread Steven D'Aprano
On Thu, 18 Sep 2008 02:26:29 +0430, Lee Harr wrote: > I have a class with certain methods from which I want to select one at > random, with weighting. > > The way I have done it is this [snip] You are coupling the weights, the actions, and the object which chooses an action all in the on

Re: decorator and API

2008-09-17 Thread Aaron "Castironpi" Brady
On Sep 17, 6:09 pm, "Aaron \"Castironpi\" Brady" <[EMAIL PROTECTED]> wrote: > On Sep 17, 4:56 pm, Lee Harr <[EMAIL PROTECTED]> wrote: > > > > > I have a class with certain methods from which I want to select > > one at random, with weighting. (snip) > > > The problem I have now is that if I subclas

Re: decorator and API

2008-09-17 Thread Aaron "Castironpi" Brady
On Sep 17, 4:56 pm, Lee Harr <[EMAIL PROTECTED]> wrote: > I have a class with certain methods from which I want to select > one at random, with weighting. > > The way I have done it is this > > import random > > def weight(value): >     def set_weight(method): >         method.weight = value >

decorator and API

2008-09-17 Thread Lee Harr
I have a class with certain methods from which I want to select one at random, with weighting. The way I have done it is this import random def weight(value): def set_weight(method): method.weight = value return method return set_weight class A(object): def a