Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > deriving from float or int

Reply
Thread Tools

deriving from float or int

 
 
Russ
Guest
Posts: n/a
 
      02-21-2006
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.

 
Reply With Quote
 
 
 
 
Sybren Stuvel
Guest
Posts: n/a
 
      02-21-2006
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
 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      02-21-2006
Probably this is interesting for you:
http://home.tiscali.be/be052320/Unum.html

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

Bye,
bearophile

 
Reply With Quote
 
Russ
Guest
Posts: n/a
 
      02-21-2006
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.

 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      02-21-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

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

 
Reply With Quote
 
Sybren Stuvel
Guest
Posts: n/a
 
      02-21-2006
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
 
Reply With Quote
 
gene tani
Guest
Posts: n/a
 
      02-22-2006

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/

 
Reply With Quote
 
Terry Hancock
Guest
Posts: n/a
 
      02-22-2006
On Tue, 21 Feb 2006 15:01:22 -0600
Robert Kern <(E-Mail Removed)> wrote:
> http://www.python.org/2.2.3/descrintro.html#__new__


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 ((E-Mail Removed))
Anansi Spaceworks http://www.AnansiSpaceworks.com

 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      02-22-2006
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
(E-Mail Removed)

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Float to int conversion by using two int variables for representation of the float variable k3n3dy C++ 15 04-20-2006 06:53 PM
Float to int conversions (was: int(float(sys.maxint)) buglet) Nick Coghlan Python 0 12-06-2004 12:12 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments