deriving from float or int

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

    #1

    deriving from float or int

    Does it ever make sense to derive a class from a basic type such as
    float or int? Suppose, for example, that I want to create a class for
    physical scalars with units. I thought about deriving from float, then
    adding the units. I played around with it a bit, but it doesn't seem to
    work very well. Am I missing something here? Thanks.

  • Sybren Stuvel

    #2
    Re: deriving from float or int

    Russ enlightened us with:[color=blue]
    > Does it ever make sense to derive a class from a basic type such as
    > float or int? Suppose, for example, that I want to create a class
    > for physical scalars with units.[/color]

    That makes sense.
    [color=blue]
    > I thought about deriving from float, then adding the units. I played
    > around with it a bit, but it doesn't seem to work very well. Am I
    > missing something here? Thanks.[/color]

    Yeah, you forgot to include a magical 'make the reader psychic' text
    ;-)

    An explanation of what you did and what went wrong would be useful in
    answering your question.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: deriving from float or int

      Probably this is interesting for you:


      I think its API can be improved, but it can be used.

      Bye,
      bearophile

      Comment

      • Russ

        #4
        Re: deriving from float or int

        The problem is that when I derive a new class from float, the darn
        thing won't let me create a constructor that accepts more than one
        argument. I need two arguments: one for the numerical value and one for
        the units. But when I try to give the constructor two arguments, I get
        this when I call the constructor:

        TypeError: float() takes at most 1 argument (2 given)

        In other words, python doesn't seem to want to let me "extend" the
        float type. I don't understand the reason for that, but I assume there
        is a reason.

        Comment

        • Robert Kern

          #5
          Re: deriving from float or int

          Russ wrote:[color=blue]
          > The problem is that when I derive a new class from float, the darn
          > thing won't let me create a constructor that accepts more than one
          > argument. I need two arguments: one for the numerical value and one for
          > the units. But when I try to give the constructor two arguments, I get
          > this when I call the constructor:
          >
          > TypeError: float() takes at most 1 argument (2 given)
          >
          > In other words, python doesn't seem to want to let me "extend" the
          > float type. I don't understand the reason for that, but I assume there
          > is a reason.[/color]



          --
          Robert Kern
          robert.kern@gma il.com

          "In the fields of hell where the grass grows high
          Are the graves of dreams allowed to die."
          -- Richard Harter

          Comment

          • Sybren Stuvel

            #6
            Re: deriving from float or int

            Russ enlightened us with:[color=blue]
            > The problem is that when I derive a new class from float, the darn
            > thing won't let me create a constructor that accepts more than one
            > argument.[/color]

            Use __new__, not __init__. It's the function that's called when a new
            immutable object is created.

            Sybren
            --
            The problem with the world is stupidity. Not saying there should be a
            capital punishment for stupidity, but why don't we just take the
            safety labels off of everything and let the problem solve itself?
            Frank Zappa

            Comment

            • Terry Hancock

              #7
              Re: deriving from float or int

              On Tue, 21 Feb 2006 15:01:22 -0600
              Robert Kern <robert.kern@gm ail.com> wrote:[color=blue]
              > http://www.python.org/2.2.3/descrintro.html#__new__[/color]

              Curiously, __new__ does not appear in the index of
              the Python 2.3 language reference!

              It is fixed in Python 2.4, though -- I just checked.

              --
              Terry Hancock (hancock@Anansi Spaceworks.com)
              Anansi Spaceworks http://www.AnansiSpaceworks.com

              Comment

              • gene tani

                #8
                Re: deriving from float or int


                Russ wrote:[color=blue]
                > Does it ever make sense to derive a class from a basic type such as
                > float or int? Suppose, for example, that I want to create a class for
                > physical scalars with units. I thought about deriving from float, then
                > adding the units. I played around with it a bit, but it doesn't seem to
                > work very well. Am I missing something here? Thanks.[/color]

                you could look at how sciPy does it:


                Comment

                • Robert Kern

                  #9
                  Re: deriving from float or int

                  gene tani wrote:[color=blue]
                  > Russ wrote:
                  >[color=green]
                  >>Does it ever make sense to derive a class from a basic type such as
                  >>float or int? Suppose, for example, that I want to create a class for
                  >>physical scalars with units. I thought about deriving from float, then
                  >>adding the units. I played around with it a bit, but it doesn't seem to
                  >>work very well. Am I missing something here? Thanks.[/color]
                  >
                  > you could look at how sciPy does it:
                  > http://starship.python.net/~hinsen/ScientificPython/[/color]

                  ScientificPytho n != SciPy

                  ScientificPytho n's unit package does not subclass from floats since it tries to
                  be agnostic about the kind of value you can assign a unit to.

                  --
                  Robert Kern
                  robert.kern@gma il.com

                  "In the fields of hell where the grass grows high
                  Are the graves of dreams allowed to die."
                  -- Richard Harter

                  Comment

                  Working...