help with object method calls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kdt
    New Member
    • Mar 2007
    • 50

    help with object method calls

    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:

    [CODE=python]
    def factorial(n):
    '''
    n is a positive integer;
    RETURNS: integer factorial of n from a recursive function.
    '''
    f = 1
    while (n > 0):
    f = f * n
    n = n - 1
    return f

    def binomial(n, p, x):
    '''
    n is a positive integer number of independent Bernoulli Trials;
    p is the probability of success of the binomial event;
    x is the positive integer number of Bernoulli Trials of n.
    RETURNS: The probability of success from a series of Bernoulli Trials
    '''
    fn = factorial(n)
    return (fn/(factorial(x)*( factorial(n-x))))*(p**x)*(( 1-p)**(n-x))

    [/CODE]

    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

    [CODE=python]

    class Numbers():
    def __init__(self, numbers):
    self.numbers = numbers

    def factorial(self) :
    f=1
    while (self.numbers>0 ):
    f*=self.numbers
    self.numbers-=1
    return f

    def binomial(self, x, p):
    fn=self.factori al()
    y=Numbers(x)
    return (fn/(y.factorial()* (y-1).factorial))# stuck here

    [/CODE]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by kdt
    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:

    [CODE=python]
    def factorial(n):
    '''
    n is a positive integer;
    RETURNS: integer factorial of n from a recursive function.
    '''
    f = 1
    while (n > 0):
    f = f * n
    n = n - 1
    return f

    def binomial(n, p, x):
    '''
    n is a positive integer number of independent Bernoulli Trials;
    p is the probability of success of the binomial event;
    x is the positive integer number of Bernoulli Trials of n.
    RETURNS: The probability of success from a series of Bernoulli Trials
    '''
    fn = factorial(n)
    return (fn/(factorial(x)*( factorial(n-x))))*(p**x)*(( 1-p)**(n-x))

    [/CODE]

    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

    [CODE=python]

    class Numbers():
    def __init__(self, numbers):
    self.numbers = numbers

    def factorial(self) :
    f=1
    while (self.numbers>0 ):
    f*=self.numbers
    self.numbers-=1
    return f

    def binomial(self, x, p):
    fn=self.factori al()
    y=Numbers(x)
    return (fn/(y.factorial()* (y-1).factorial))# stuck here

    [/CODE]
    This seems to work. You may need to do some error trapping. [code=Python]class Numbers(object) :
    def __init__(self, numbers):
    self.numbers = numbers

    def factorial(self, x):
    f=1
    while (x>0):
    f*=x
    x-=1
    return f

    def binomial(self, x, p):
    fn=self.factori al(self.numbers )
    return (float(fn)/(self.factorial (x)*\
    (self.factorial (self.numbers-x)-1))
    )*(p**x)*((1-p)**(self.numbe rs-x)[/code]

    Comment

    • kdt
      New Member
      • Mar 2007
      • 50

      #3
      Originally posted by bvdet
      This seems to work. You may need to do some error trapping. [code=Python]class Numbers(object) :
      def __init__(self, numbers):
      self.numbers = numbers

      def factorial(self, x):
      f=1
      while (x>0):
      f*=x
      x-=1
      return f

      def binomial(self, x, p):
      fn=self.factori al(self.numbers )
      return (float(fn)/(self.factorial (x)*\
      (self.factorial (self.numbers-x)-1))
      )*(p**x)*((1-p)**(self.numbe rs-x)[/code]
      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

      [CODE=python]
      class Number(object):
      def __init__(self, number):
      self.number = number

      def factorial(self, x):
      f=1
      while (x>0):
      f*=x
      x-=1
      return f

      def binomial(self, x, p):
      fn=float(self.f actorial(self.n umber))
      return (fn/(self.factorial (x)*\
      (self.factorial (self.number-x))))*\
      (p**x)*((1-p)**(self.numbe r-x))
      def __str__(self):
      return str(self.number )


      n = Number(10)
      print n.factorial(6)
      print n.binomial(3, 0.8)

      >>>
      720
      0.000786432
      [/CODE]

      Comment

      Working...