__div__ not recognized automatically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anton81

    __div__ not recognized automatically

    Hello!

    I wrote a class

    class NumX:
    ...
    def __add__(self,ot her):
    ...
    def __div__(self,ot her):
    if not isinstance(othe r,NumX): other=NumX(othe r)
    ...

    Somewhere else I use

    a=(b+c)/2

    where all variables are of NumX Type. When I execute the program it
    complains that it can't find an operator "/" for "instance" and "integer".
    However if I use pdb the same command works when started on the prompt. Also
    the manual execution

    a=(b+c).__div__ (2)

    works. Any suggestions what goes wrong?

    Anton
  • Steven D'Aprano

    #2
    Re: __div__ not recognized automatically

    On Thu, 02 Nov 2006 12:59:32 +0100, Anton81 wrote:
    When I execute the program it
    complains that it can't find an operator "/" for "instance" and "integer".
    How about if you post the actual traceback you get, rather than
    paraphrasing? That way, we don't have to guess.


    --
    Steven.

    Comment

    • Nick Craig-Wood

      #3
      Re: __div__ not recognized automatically

      Anton81 <usenet1@anton. e4ward.comwrote :
      class NumX:
      ...
      def __add__(self,ot her):
      ...
      def __div__(self,ot her):
      if not isinstance(othe r,NumX): other=NumX(othe r)
      ...
      >
      Somewhere else I use
      >
      a=(b+c)/2
      >
      where all variables are of NumX Type. When I execute the program it
      complains that it can't find an operator "/" for "instance" and "integer".
      However if I use pdb the same command works when started on the prompt. Also
      the manual execution
      >
      a=(b+c).__div__ (2)
      >
      works. Any suggestions what goes wrong?
      Post some code which actually demonstrates the problem.

      Eg, change this code until it does demonstrate the problem

      ------------------------------------------------------------
      class NumX(object):
      def __init__(self, value):
      self.value = long(value)

      def __add__(self,ot her):
      if not isinstance(othe r,NumX): other=NumX(othe r)
      return NumX(self.value + other.value)

      def __div__(self,ot her):
      if not isinstance(othe r,NumX): other=NumX(othe r)
      return NumX(self.value / other.value)

      def __str__(self):
      return "%s(%s)" % (self.__class__ .__name__, self.value)

      a = NumX(4)
      b = NumX(2)

      print a,b
      print a+b
      print (a+b)/2
      ------------------------------------------------------------

      This prints

      ------------------------------------------------------------
      NumX(4) NumX(2)
      NumX(6)
      NumX(3)
      ------------------------------------------------------------

      --
      Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

      Comment

      • Peter Otten

        #4
        Re: __div__ not recognized automatically

        Anton81 wrote:
        Hello!
        >
        I wrote a class
        >
        class NumX:
        ...
        def __add__(self,ot her):
        ...
        def __div__(self,ot her):
        if not isinstance(othe r,NumX): other=NumX(othe r)
        ...
        >
        Somewhere else I use
        >
        a=(b+c)/2
        >
        where all variables are of NumX Type. When I execute the program it
        complains that it can't find an operator "/" for "instance" and "integer".
        However if I use pdb the same command works when started on the prompt.
        Also the manual execution
        >
        a=(b+c).__div__ (2)
        >
        works. Any suggestions what goes wrong?
        If you have the

        from __future__ import division

        statement, you need to override __truediv__(), not __div__()

        Peter

        Comment

        • Tuomas

          #5
          Re: __div__ not recognized automatically


          Try
          a=(b+c)/NumX(2)

          TV

          Anton81 wrote:
          Hello!
          >
          I wrote a class
          >
          class NumX:
          ...
          def __add__(self,ot her):
          ...
          def __div__(self,ot her):
          if not isinstance(othe r,NumX): other=NumX(othe r)
          ...
          >
          Somewhere else I use
          >
          a=(b+c)/2
          >
          where all variables are of NumX Type. When I execute the program it
          complains that it can't find an operator "/" for "instance" and "integer".
          However if I use pdb the same command works when started on the prompt. Also
          the manual execution
          >
          a=(b+c).__div__ (2)
          >
          works. Any suggestions what goes wrong?
          >
          Anton

          Comment

          • Anton81

            #6
            Re: __div__ not recognized automatically

            If you have the
            >
            from __future__ import division
            >
            statement, you need to override __truediv__(), not __div__()
            That worked after I also added
            from __future__ import division
            to all other modules I created.

            Is it possible that there appears an inconsistency if the division is
            imported in only some of the modules?

            Anton

            Comment

            • Peter Otten

              #7
              Re: __div__ not recognized automatically

              Anton81 wrote:
              >If you have the
              >>
              >from __future__ import division
              >>
              >statement, you need to override __truediv__(), not __div__()
              >
              That worked after I also added
              from __future__ import division
              to all other modules I created.
              >
              Is it possible that there appears an inconsistency if the division is
              imported in only some of the modules?
              Yes. If you use your class with and without the __future__ option you have
              to implement both __div__ and __truediv__, I suppose.

              Peter


              Comment

              Working...