On Fri, Jan 23, 2009 at 6:04 AM, spir <[email protected]> wrote:
> Thank you Alan and sorry for not having been clear enough. The point actually
> was class (definition) attributes. I thought at e.g. Guido's views that lists
> were for homogeneous sequences as opposed to tuples rather like records. And
> a way to ensure sich a homogeneity, in the sense of items beeing of the same
> type or super type.
> The straightforward path to ensure that, as I see it, is to add proper
> argument to a class definition.
A simple way to do this is with a class factory function, for example:
def makeMonoList(typ, number):
class MonoListSubtype(MonoList):
item_type = type
item_number = number
return MonoListSubtype
then e.g.
IntegerList = makeMonoList(int, 5)
myIntegerList = IntegerList()
This is similar in spirit to collections.namedtuple() in Python 2.6
though the implementation is different; namedtuple() actually creates
and evaluates the text of the new class definition:
http://docs.python.org/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields
http://svn.python.org/view/python/trunk/Lib/collections.py?rev=68853&view=auto
Kent
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor