deriving from float or int

R

Russ

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

Sybren Stuvel

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
 
R

Russ

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

Robert Kern

Russ said:
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
(e-mail address removed)

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

Sybren Stuvel

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
 
G

gene tani

Russ said:
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/
 
R

Robert Kern

gene said:

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 address removed)

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top