473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.

Feb 21 '06 #1
8 2471
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
Feb 21 '06 #2
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

Feb 21 '06 #3
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.

Feb 21 '06 #4
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
ro*********@gmail.com

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

Feb 21 '06 #5
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
Feb 21 '06 #6
On Tue, 21 Feb 2006 15:01:22 -0600
Robert Kern <ro*********@gmail.com> 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 (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Feb 22 '06 #7

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/

Feb 22 '06 #8
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
ro*********@gmail.com

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

Feb 22 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Noah Roberts | last post by:
I am having some problems with deriving the std::exception class. My compiler (g++-2.95) works with it just fine, but it does in a lot of broken cases. I have a user/developer that can't compile...
3
by: Hans De Winter | last post by:
Hi, I am really puzzled why my compiler does not accept the following code:- --- CODE STARTS HERE --- #include <iostream> using namespace std; class Foo {
28
by: Steven T. Hatton | last post by:
This may be another question having an obvious answer, but I'm not seeing it. I'm trying to create a class that derives from std::valarray<std::string>. I don't need a template, and I haven't come...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
7
by: Schüle Daniel | last post by:
Hello I am trying to customize the handling of complex numbers what I am missing is a builtin possibility to create complex numbers in polar coordinates so first I wrote a standalone function...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
1
by: Christopher Pisz | last post by:
I set out to make a custom logger. Examining some other code laying around, I came across one that derived from ostream, and had a associated class derived from streambuf. Is this practice a good...
0
by: James Kanze | last post by:
On 11 avr, 17:44, "mc" <mc_r...@yahoo.comwrote: OK. If the actual format is well documented, that's half the battle won already. Note, however, that reading a float as an int is still very...
3
by: Al Grant | last post by:
Consider two translation units, (1): namespace { struct S { }; } struct D: S { virtual int f(void); }; and (2): #include <typeinfo> struct D; char const *f(D *p) { return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.