473,241 Members | 1,538 Online

# help with object method calls

50
Hi,

Been reading about objects today. I have a program, that I am trying to rewrite as much as possible as objects. I want to write the following two functions as methods in the Number class:

Expand|Select|Wrap|Line Numbers
1. def factorial(n):
2.     '''
3.     n is a positive integer;
4.     RETURNS: integer factorial of n from a recursive function.
5.     '''
6.     f = 1
7.     while (n > 0):
8.         f = f * n
9.         n = n - 1
10.     return f
11.
12. def binomial(n, p, x):
13.     '''
14.     n is a positive integer number of independent Bernoulli Trials;
15.         p is the probability of success of the binomial event;
16.         x is the positive integer number of Bernoulli Trials of n.
17.     RETURNS: The probability of success from a series of Bernoulli Trials
18.     '''
19.     fn = factorial(n)
20.     return (fn/(factorial(x)*(factorial(n-x))))*(p**x)*((1-p)**(n-x))
21.
22.
I am stuck on the last line of the binomial, as I seem to have to instantiate x and n-x as Numbers. Is this right? Or is it better to not make objects out of them? Thanks

Expand|Select|Wrap|Line Numbers
1.
2. class Numbers():
3.     def __init__(self, numbers):
4.         self.numbers = numbers
5.
6.     def factorial(self):
7.         f=1
8.         while (self.numbers>0):
9.             f*=self.numbers
10.             self.numbers-=1
11.         return f
12.
13.     def binomial(self, x, p):
14.         fn=self.factorial()
15.         y=Numbers(x)
16.         return (fn/(y.factorial()* (y-1).factorial))# stuck here
17.
18.
Sep 21 '07 #1
2 1173
bvdet
2,851 Expert Mod 2GB
Hi,

Been reading about objects today. I have a program, that I am trying to rewrite as much as possible as objects. I want to write the following two functions as methods in the Number class:

Expand|Select|Wrap|Line Numbers
1. def factorial(n):
2.     '''
3.     n is a positive integer;
4.     RETURNS: integer factorial of n from a recursive function.
5.     '''
6.     f = 1
7.     while (n > 0):
8.         f = f * n
9.         n = n - 1
10.     return f
11.
12. def binomial(n, p, x):
13.     '''
14.     n is a positive integer number of independent Bernoulli Trials;
15.         p is the probability of success of the binomial event;
16.         x is the positive integer number of Bernoulli Trials of n.
17.     RETURNS: The probability of success from a series of Bernoulli Trials
18.     '''
19.     fn = factorial(n)
20.     return (fn/(factorial(x)*(factorial(n-x))))*(p**x)*((1-p)**(n-x))
21.
22.
I am stuck on the last line of the binomial, as I seem to have to instantiate x and n-x as Numbers. Is this right? Or is it better to not make objects out of them? Thanks

Expand|Select|Wrap|Line Numbers
1.
2. class Numbers():
3.     def __init__(self, numbers):
4.         self.numbers = numbers
5.
6.     def factorial(self):
7.         f=1
8.         while (self.numbers>0):
9.             f*=self.numbers
10.             self.numbers-=1
11.         return f
12.
13.     def binomial(self, x, p):
14.         fn=self.factorial()
15.         y=Numbers(x)
16.         return (fn/(y.factorial()* (y-1).factorial))# stuck here
17.
18.
This seems to work. You may need to do some error trapping.
Expand|Select|Wrap|Line Numbers
1. class Numbers(object):
2.     def __init__(self, numbers):
3.         self.numbers = numbers
4.
5.     def factorial(self, x):
6.         f=1
7.         while (x>0):
8.             f*=x
9.             x-=1
10.         return f
11.
12.     def binomial(self, x, p):
13.         fn=self.factorial(self.numbers)
14.         return (float(fn)/(self.factorial(x)*\
15.                     (self.factorial(self.numbers-x)-1))
16.                 )*(p**x)*((1-p)**(self.numbers-x)
Sep 21 '07 #2
kdt
50
This seems to work. You may need to do some error trapping.
Expand|Select|Wrap|Line Numbers
1. class Numbers(object):
2.     def __init__(self, numbers):
3.         self.numbers = numbers
4.
5.     def factorial(self, x):
6.         f=1
7.         while (x>0):
8.             f*=x
9.             x-=1
10.         return f
11.
12.     def binomial(self, x, p):
13.         fn=self.factorial(self.numbers)
14.         return (float(fn)/(self.factorial(x)*\
15.                     (self.factorial(self.numbers-x)-1))
16.                 )*(p**x)*((1-p)**(self.numbers-x)
Thanks bvdet, was struggling with this yesterday. There's an extra '-1' in your code, but apart from that, it works fine. The only obscurity is having to pass two parameters for the factorial method - although I understand that this is required for it to be able to be called from the binomail method. Is it better practice to keep it like this, or should it be left in functions? Just looking for an opinion here, still a newb so want to learn the best practices. Cheers

Expand|Select|Wrap|Line Numbers
1. class Number(object):
2.     def __init__(self, number):
3.         self.number = number
4.
5.     def factorial(self, x):
6.         f=1
7.         while (x>0):
8.             f*=x
9.             x-=1
10.         return f
11.
12.     def binomial(self, x, p):
13.         fn=float(self.factorial(self.number))
14.         return (fn/(self.factorial(x)*\
15.                            (self.factorial(self.number-x))))*\
16.                            (p**x)*((1-p)**(self.number-x))
17.     def __str__(self):
18.         return str(self.number)
19.
20.
21. n = Number(10)
22. print n.factorial(6)
23. print n.binomial(3, 0.8)
24.
25. >>>
26. 720
27. 0.000786432
28.
Sep 22 '07 #3