deriving from float or int

Discussion in 'Python' started by Russ, Feb 21, 2006.

  1. Russ

    Russ Guest

    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.
    Russ, Feb 21, 2006
    #1
    1. Advertising

  2. Russ enlightened us with:
    > 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.


    That makes sense.

    > 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.


    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
    Sybren Stuvel, Feb 21, 2006
    #2
    1. Advertising

  3. Russ

    Guest

    , Feb 21, 2006
    #3
  4. Russ

    Russ Guest

    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.
    Russ, Feb 21, 2006
    #4
  5. Russ

    Robert Kern Guest

    Russ wrote:
    > 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.


    http://www.python.org/2.2.3/descrintro.html#__new__

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Feb 21, 2006
    #5
  6. Russ enlightened us with:
    > 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.


    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
    Sybren Stuvel, Feb 21, 2006
    #6
  7. Russ

    gene tani Guest

    Russ wrote:
    > 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.


    you could look at how sciPy does it:
    http://starship.python.net/~hinsen/ScientificPython/
    gene tani, Feb 22, 2006
    #7
  8. Terry Hancock, Feb 22, 2006
    #8
  9. Russ

    Robert Kern Guest

    gene tani wrote:
    > Russ wrote:
    >
    >>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.

    >
    > you could look at how sciPy does it:
    > http://starship.python.net/~hinsen/ScientificPython/


    ScientificPython != SciPy

    ScientificPython'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


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Feb 22, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,199
    Martien Verbruggen
    Jun 27, 2003
  2. bd
    Replies:
    0
    Views:
    608
  3. Nick Coghlan
    Replies:
    0
    Views:
    470
    Nick Coghlan
    Dec 6, 2004
  4. k3n3dy
    Replies:
    15
    Views:
    977
    dan2online
    Apr 20, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,509
    James Kanze
    Oct 8, 2009
Loading...

Share This Page