subtype and super method. How to?

Prashant Saxena animator333 at yahoo.com
Thu Jul 17 06:29:08 CEST 2008


import sys

class Attribute(object):
    """
    Common attributes and methods for custom types
    """
    __slots__ = []
    
    def __init__(self, name=None, type=None, range=(0,1)):
        self.__name = name
        self.__type = type
        self.__range = range
    
    #Read only attributes
    name = property(lambda self: self.__name)
    type = property(lambda self: self.__type)
    range = property(lambda self: self.__range)
        
class Float(float, Attribute):
    '''Custom Float type'''
    __slots__ = ('__name', '__type', '__range')
    def __new__(self, value=0.0, name=None, type=None, range=(0.0, 1.0)):
        try:
            super(Float, self).__init__(name=name, type=type, range=range)
            return float.__new__(self, value)
        except:
            print 'Error : %s %s' % sys.exc_info()[:2]
                
class Int(int, Attribute):
    '''Custom Int type'''
    __slots__ = ('__name', '__type', '__range')
    def __new__(self, value=0, name=None, type=None, range=(0, 1)):
        try:
            super(Int, self).__init__(name=name, type=type, range=range)
            return int.__new__(self, value)
        except:
            print 'Error : %s %s' % sys.exc_info()[:2]

a = Float(2.0, name='myFloat')
print a

'Float' & 'Int' instances are not getting initialize. How to solve?

Thanks



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


More information about the Python-list mailing list