[Tutor] listing classes

Kent Johnson kent37 at tds.net
Thu May 22 14:37:02 CEST 2008


On Wed, May 21, 2008 at 10:41 PM, Laureano Arcanio
<listas.condhor at gmail.com> wrote:

> The problem comes because i need to keep the order of the HTML tags, and as
> you say dict doesn't work like that.. I've working on this metaclass, and
> then extend list with it, but i have the same problem, the dct comes in a
> dict...
>
> class MetaHTML(type):
>     def __new__(meta, name , bases, dct):
>         # Deletes methods and attributes containing "_"
>         items = []
>         for key, value in dct.items():
>             if '_' in key:
>                 dct.pop(key)
>
>         items = [tag() for tag in dct.values()]
>
>         def __init__(self, items=items):
>             self.extend(items)
>         dct.update({'__slots__':[], '__init__':__init__})
>         return type.__new__(meta,name,bases,dct)
>
> class HTML(list):
>         __metaclass__ = MetaHTML

The *nested* classes need a metaclass that keeps track of order. At
the point of creation of the nested class object, you can add the
object to a list. I think you can do this with a metaclass...

Kent


More information about the Tutor mailing list