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

K

King

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))
 
P

Peter Otten

King said:
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.

There may be some technical errors in your code, e. g.

- the __slots__ look like a bit of bullshit: you already have a __dict__
from the Attribute base class
- name mangling is done for attributes starting with two underscores

But the greater problem I see is that you are building up a huge bureaucracy
where pythonic code would concentrate on a concrete goal with minimal
administrative overhead.

Peter
 
K

King

My mistake...
The correct __slots__ is like:
__slots__ = ['_Attribute_name', '_Attribute_type', '_Attribute_range',
'label']

Could you please suggest an alternative or code improvement for the
matter.

Prashant
 
P

Peter Otten

King said:
My mistake...
The correct __slots__ is like:
__slots__ = ['_Attribute_name', '_Attribute_type', '_Attribute_range',
'label']

Could you please suggest an alternative or code improvement for the
matter.

How about

class AttributesMixin(object):
"""Warning: If you change name, type, or range of an existing object
your computer will explode and you will have noone else to blame.
"""
def __init__(self, name=None, type=None, range=None, label=None):
self.name = name
self.type = type
self.range = range
self.label = label

class Float(float, AttributesMixin): pass
class Int(int, AttributesMixin): pass

Now go and write something useful :)

Peter
 
M

Maric Michaud

Le Friday 18 July 2008 11:36:20 King, vous avez écrit :
Could you please suggest an alternative or code improvement for the
matter.

I'm not sure what you are trying to achieve with your snippet, but I suspect
it's some sort of templating, right ? If so, using the dynamic nature of
python should help :
[103]: def make_subtype_with_attr(type_, ro_attr, rw_attr) :
dic = {}
for i in ro_attr : dic = property(lambda s, n=i : getattr(s, '_'+n))
def __new__(cls, *args, **kwargs) :
instance = type_.__new__(cls, *args)
for i in rw_attr : setattr(instance, i, kwargs)
for i in ro_attr : setattr(instance, '_'+i, ro_attr)
return instance
dic['__new__'] = __new__
return type('my_' + type_.__name__, (type_,), dic)
.....:
[113]: my_int = make_subtype_with_attr(int,
{'name' : 'myint', 'id':123452}, ('foo',))
[114]: i = my_int(5, foo=3)
[115]: i.foo
...[115]: 3
...[116]: 5
...[117]: 123452
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)

/home/maric/<ipython console> in <module>()

AttributeError: can't set attribute
 
K

King

Thanks Michaud,

You have actually solved an another problem by
'make_subtype_with_attr' function.

What actually I am trying to do here is to create custom types using
built in types to use in my application.
For example , float2, float3, color, vector, point, normal, matrix
etc. The reason being creating as individual subtypes is because each
subtypes has it's own set of functions as well as common functions and
attributes defined in 'Attribute' class.

Although I won't be doing any math using subtypes like multiply a
color by another color or add a float and a color. One of the
important function that every subtype has is to print itself in a
special format. For 'color' it would be :

'type' + 'name' + ' = color('+color[0]+', '+color[2]+',
'+color[2]+');'
Result: color myColor = color(0.5, 0.1, 1.0);

I hope this will make things more clearer. If you do have any
suggestions then please send it across.

Prashant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top