sub typing built in type with common attributes. Am I right?

King animator333 at gmail.com
Fri Jul 18 03:32:46 EDT 2008


I am new to python and getting into classes. Here I am trying to
create subtype of built in types with some additional attributes and
function. 'Attributes' is the main class and holds all the common
attributes that requires by these subtypes. This is what I came out
with. I need to know whether everything is right here ? or there are
some  better ways to do it.

class Attribute(object):
    """
    Common attributes and methods for custom types
    """

    def __init__(self, name=None, type=None, range=None, label=None):

        #read only attributes
        self._name = name
        self._type = type
        self._range = range

        #read/write attribute
        self.label = label

    #make 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 class with some additional attributes.'''

    __slots__ = ['_Attribute__name', '_Attribute__type',
'_Attribute__range', 'label']

    def __new__(cls, value=0.0, *args, **kwargs):
        return float.__new__(cls, value)

    def __init__(self, value=0.0, name=None, label=None, type=None,
range=(0.0, 1.0)):
        super(Float, self).__init__(name=name, label=label, type=type,
range=range)

    def __str__(self):
        return '{ %s }' % (float.__str__(self))

class Int(int, Attribute):

    '''Custom Int class with some additional attributes.'''

    __slots__ = ['_Attribute__name', '_Attribute__type',
'_Attribute__range', 'label']

    def __new__(cls, value=0.0, *args, **kwargs):
        return int.__new__(cls, value)

    def __init__(self, value=0.0, name=None, label=None, type=None,
range=(0.0, 1.0)):
        super(Int, self).__init__(name=name, label=label, type=type,
range=range)

    def __str__(self):
        return '{ %s }' % (int.__str__(self))



More information about the Python-list mailing list