By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,536 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

Wrapping float

P: n/a
Hi all,

I'm trying to make a float-like class (preferably a subclass of
'float') that wraps around. The background: I'm modeling a
multi-dimensional space, and some of those dimensions are circular.

Here is my code so far:

class WrapFloat(float):
def __init__(self, value, wrap = None):
float.__init__(self, value)
self.wrap = wrap

The problem is this:

Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
[GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from engine.geometry import WrapFloat
WrapFloat(45) 45.0 WrapFloat(45, 3)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: float() takes at most 1 argument (2 given)

So my question to you is: how can I change my code so I can pass two
values to the WrapFloat constructor?

Thanks in advance,
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
Sep 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sat, 17 Sep 2005 11:12:34 +0200, Sybren Stuvel <sy****@localhost.localdomain> wrote:
Hi all,

I'm trying to make a float-like class (preferably a subclass of
'float') that wraps around. The background: I'm modeling a
multi-dimensional space, and some of those dimensions are circular.

Here is my code so far:

class WrapFloat(float):
def __init__(self, value, wrap = None):
float.__init__(self, value)
self.wrap = wrap

The problem is this:

Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
[GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from engine.geometry import WrapFloat
WrapFloat(45)45.0 WrapFloat(45, 3)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: float() takes at most 1 argument (2 given)

So my question to you is: how can I change my code so I can pass two
values to the WrapFloat constructor?

Float is an immutable, so you need to override __new__

Regards,
Bengt Richter
Sep 17 '05 #2

P: n/a
Sybren Stuvel wrote:
Hi all,

I'm trying to make a float-like class (preferably a subclass of
'float') that wraps around. The background: I'm modeling a
multi-dimensional space, and some of those dimensions are circular.

Here is my code so far:

class WrapFloat(float):
def __init__(self, value, wrap = None):
float.__init__(self, value)
self.wrap = wrap

The problem is this:

Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
[GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from engine.geometry import WrapFloat
WrapFloat(45)
45.0
WrapFloat(45, 3)


Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: float() takes at most 1 argument (2 given)

So my question to you is: how can I change my code so I can pass two
values to the WrapFloat constructor?


You also have to override __new__ I think. It automatically gets passed
the arguments to __init__. C.f.
http://www.python.org/2.2/descrintro.html#__new__

In [11]: class WrapFloat(float):
....: def __new__(cls, value, *args, **kwds):
....: return float.__new__(cls, value)
....: def __init__(self, value, wrap=None):
....: float.__init__(self, value)
....: self.wrap = wrap
....:

In [12]: x = WrapFloat(45, 3)

In [13]: x
Out[13]: 45.0

In [14]: x.wrap
Out[14]: 3

--
Robert Kern
rk***@ucsd.edu

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

Sep 17 '05 #3

P: n/a
Bengt Richter enlightened us with:
Float is an immutable, so you need to override __new__


Thanks, that works!

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
Sep 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.