Wrapping float

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sybren Stuvel

    Wrapping float

    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.[color=blue][color=green][color=darkred]
    >>> from engine.geometry import WrapFloat
    >>> WrapFloat(45)[/color][/color][/color]
    45.0[color=blue][color=green][color=darkred]
    >>> WrapFloat(45, 3)[/color][/color][/color]
    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
  • Bengt Richter

    #2
    Re: Wrapping float

    On Sat, 17 Sep 2005 11:12:34 +0200, Sybren Stuvel <sybren@localho st.localdomain> wrote:
    [color=blue]
    >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.[color=green][color=darkred]
    >>>> from engine.geometry import WrapFloat
    >>>> WrapFloat(45)[/color][/color]
    >45.0[color=green][color=darkred]
    >>>> WrapFloat(45, 3)[/color][/color]
    >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?
    >[/color]
    Float is an immutable, so you need to override __new__

    Regards,
    Bengt Richter

    Comment

    • Robert Kern

      #3
      Re: Wrapping float

      Sybren Stuvel wrote:[color=blue]
      > 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.
      >[color=green][color=darkred]
      >>>>from engine.geometry import WrapFloat
      >>>>WrapFloat(4 5)[/color][/color]
      >
      > 45.0
      >[color=green][color=darkred]
      >>>>WrapFloat(4 5, 3)[/color][/color]
      >
      > 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?[/color]

      You also have to override __new__ I think. It automatically gets passed
      the arguments to __init__. C.f.
      The official home of the Python Programming Language


      In [11]: class WrapFloat(float ):
      ....: def __new__(cls, value, *args, **kwds):
      ....: return float.__new__(c ls, 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
      rkern@ucsd.edu

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

      Comment

      • Sybren Stuvel

        #4
        Re: Wrapping float

        Bengt Richter enlightened us with:[color=blue]
        > Float is an immutable, so you need to override __new__[/color]

        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

        Comment

        Working...