Play with classes

Jacek Generowicz jacek.generowicz at cern.ch
Thu Feb 26 13:18:56 CET 2004


Jacek Generowicz <jacek.generowicz at cern.ch> writes:

> ======================================================
> class TypeA: pass
> class TypeB: pass
> class TypeC: pass
> class SubtypeA: pass
> class SubtypeB: pass
> 
> collect_bases = [(Mode, Type, Subtype)
>                  for Mode in [ModeA, ModeB]
>                  for Type in [TypeA, TypeB, TypeC]
>                  for Subtype in [SubtypeA, SubtypeB]]
> 
> count = 0
> for bases in collect_bases:
>     name = "UserClass%d" % count
>     the_class = type(name, bases, {})
>     globals()[name] = the_class
>     count += 1
> ======================================================

Oooops, the first 4 lines got lost:

======================================================
__metaclass__ = type

class ModeA: pass
class ModeB: pass
class TypeA: pass
class TypeB: pass
class TypeC: pass
class SubtypeA: pass
class SubtypeB: pass

collect_bases = [(Mode, Type, Subtype)
                 for Mode in [ModeA, ModeB]
                 for Type in [TypeA, TypeB, TypeC]
                 for Subtype in [SubtypeA, SubtypeB]]

count = 0
for bases in collect_bases:
    name = "UserClass%d" % count
    globals()[name] = type(name, bases, {})
    count += 1
======================================================



More information about the Python-list mailing list