class factory question

Joshua Landau joshua.landau.ws at gmail.com
Thu Jun 27 09:16:50 EDT 2013


On 26 June 2013 14:09, Tim <jtim.arnold at gmail.com> 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?

I've just realised -- why on earth are you doing this? Surely there's
a better way than having 50 identical classes. :/



More information about the Python-list mailing list