Custom 'Float' class. Am I right here?

Prashant Saxena animator333 at yahoo.com
Wed Jul 16 10:53:13 EDT 2008


import sys

class Float(float):
    """
    Custom float datatype with addtional attributes.
    """

    def __new__(self,
                        value=0.0, #default value
                        name='', # string
                        range=(0.0, 1.0) # tuple
                        )
                        
        try:
            self.name = name
            self.range = range
            self.pos = (1, 1)
            return float.__new__(self, value)
        except:
            print ('Invalid value : Float = %s %s' % (str(value), sys.exc_info()[:2]))

    
    def setpos(self, value):
        self.pos = value
    def getpos(self):
        return self.pos*2.0
    p = property(getpos, setpos)

myFloat = Float(0.23)
print myFloat.pos

I am trying to create a custom 'float' datatype by subtyping default 'float'. There are few things I would like to know here:

1. How to make 'name' & 'range' only readable attributes.
2. The property 'p' is not working as it has to. What's wrong here?
3. I would be heavily instancing this class, and I won't be creating any new attribute on instance. Is there any way to optimize? __slots__?

Thanks



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080716/074792ba/attachment.html>


More information about the Python-list mailing list