[Tutor] class arguments?

Kent Johnson kent37 at tds.net
Fri Jan 23 12:45:04 CET 2009


On Fri, Jan 23, 2009 at 6:04 AM, spir <denis.spir at free.fr> 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


More information about the Tutor mailing list