Re: Class definition within function
Peter Otten wrote: > By the way you get an instance of a different class C every time you call f, > so that > > isinstance(f(), type(f()) > > is False. That I didn't know. Well, that theory won't be seeing much practice I guess. -- Tomi Lindberg -- http://mail.python.org/mailman/listinfo/python-list
Re: Class definition within function
Diez B. Roggisch wrote: > No, its not. Only inside of it. And the question really is: why? Thanks. And no need to worry, the question was intended as fully theoretical. -- Tomi Lindberg -- http://mail.python.org/mailman/listinfo/python-list
Class definition within function
Hi, With the following function definition, is it possible to create an instance of class C outside the function f (and if it is, how)? And yes, I think this is one of those times when the real question is why :) >>> def f(): class C(object): def __init__(self): self.a = 'a' return C() >>> x = f() >>> x.a 'a' >>> y=f.C() Traceback (most recent call last): File "", line 1, in -toplevel- y=f.C() AttributeError: 'function' object has no attribute 'C' >>> -- Tomi Lindberg -- http://mail.python.org/mailman/listinfo/python-list
Re: Dice probability problem
Antoon Pardon wrote: > def __rmul__(self, num): > tp = num * [self] > return reduce(operator.add, tp) > > sum3d6 = 3 * D(6) One basic question: is there any particular reason not to use __mul__ instead (that would allow me to use both 3 * D(6) and D(6) * 3, while __rmul__ raises an AttributeError with the latter)? Difference between the two methods is slightly unclear to me. Thanks, Tomi Lindberg -- http://mail.python.org/mailman/listinfo/python-list
Re: Dice probability problem
Antoon Pardon wrote: > IMO you are making things too complicated and not general > enough. I believe that the above is very likely more than just your opinion :) Programming is just an occasional hobby to me, and I lack both experience and deeper (possibly a good chunk of shallow as well) knowledge on the subject. I'll study the code you posted, and make further questions if something remains unclear afterwards. Thanks, Tomi Lindberg -- http://mail.python.org/mailman/listinfo/python-list
Re: Dice probability problem
First, thanks to Antoon and Alexander for replying. Antoon Pardon wrote: > It would be better to construct distributions for one > die and make a function that can 'add' two distributions > together. As both replies pointed to this direction, I tried to take that route. Here's the unpolished code I came up with. Does it look even remotely sane way to accomplish my goal? -- code begins -- # A die with n faces D = lambda n: [x+1 for x in range(n)] # A new die with 6 faces d6 = D(6) # Adds another die to results. def add_dice(sums, die): # If first die, all values appear once if not sums: for face in die: sums[face] = 1 # Calculating the number of appearances for additional # dice else: new_sums = {} for k in sums.keys(): for f in die: if new_sums.has_key(k+f): new_sums[k+f] += sums[k] else: new_sums[k+f] = sums[k] sums = new_sums return sums sums = add_dice({}, d6) sums = add_dice(sums, d6) sums = add_dice(sums, d6) -- code ends -- Thanks, Tomi Lindberg -- http://mail.python.org/mailman/listinfo/python-list
Dice probability problem
Hi, I'm trying to find a way to calculate a distribution of outcomes with any combination of dice. I have the basics done, but I'm a bit unsure how to continue. My main concern is how to make this accept any number of dice, without having to write a new list comprehension for each case? Here's a piece of code that shows the way I'm doing things at the moment. -- code begins -- # A die with n faces D = lambda n: [x+1 for x in range(n)] # A pool of 3 dice with 6 faces each pool = [D(6)] * 3 # A List of all outcomes with the current 3d6 pool. results = [x+y+z for x in pool[0] for y in pool[1] for z in pool[2]] # A dictionary to hold the distribution distribution = {} # If outcome is already a key, adds 1 to its value. # Otherwise adds outcome to keys and sets its value # to 1. def count(x): if distribution.has_key(x): distribution[x] += 1 else: distribution[x] = 1 # Maps the results with above count function. map(count, results) -- code ends -- Thanks, Tomi Lindberg -- http://mail.python.org/mailman/listinfo/python-list
Re: Unit tests in Leo
[EMAIL PROTECTED] wrote: Hello Tomi, I'm not really sure about your question, but concerning unit testing you can do a simple test. No problem. I found the Leo forums at Source Forge and I've already received some answers in there. Thanks for replying though. -- "Nowhere in me is the presence of god Nor do I need him or want him around" Deicide - Standing in the Flames --- If you wish to send me an e-mail, remove .NO_SPAM and .invalid from the address. -- http://mail.python.org/mailman/listinfo/python-list
Unit tests in Leo
I'm quite (or very) new to both unit testing and Leo. I've been trying to get @test nodes to work without success so I'd like to have very simple example. So, if I have a @file with the following content... def divide_by_two(x): return x/2 ...and I'd like to write a @test node that checks whether the function returns the right number ( like assertsEqual(divide_by_two(8), 4) ). Now, what should the @test node look like? -- "Nowhere in me is the presence of god Nor do I need him or want him around" Deicide - Standing in the Flames --- If you wish to send me an e-mail, remove .NO_SPAM and .invalid from the address. -- http://mail.python.org/mailman/listinfo/python-list