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
-- http://mail.python.org/mailman/listinfo/python-list