class factory question

Peter Otten __peter__ at web.de
Wed Jun 26 09:39:12 EDT 2013


Tim wrote:

> I am extending a parser and need to create many classes that are all
> subclassed from the same object (defined in an external library).  When my
> module is loaded I need all the classes to be created with a particular
> name but the behavior is all the same. Currently I have a bunch of lines
> like this:
> 
>     class Vspace(Base.Command): pass
>     class Boldpath(Base.Command): pass
> 
> There are a bunch of lines like that.
> Is there a better way? Something like
>   
>     newclasses = ['Vspace', 'Boldpath', ... ]
>     for name in newclasses:
>         tmp = type(name, (Base.Command,) {})
>         tmp.__name__ = name
> 
> Is there a more pythonic way?

What is your objection against that approach?
By the way, I don't think you need

>         tmp.__name__ = name





More information about the Python-list mailing list