top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

class factory question using python

0 votes

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?

posted Jun 26, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

0 votes

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

  tmp.__name__ = name
answer Jun 26, 2013 by anonymous
Similar Questions
0 votes

Sorry for the vague title. Probably best to just show you the code that explains it better.

This is a simplified example of what I want to do:

from random import choice

class Expr(object):
 Expr(expr, op, val) -> an expression object.

 def __init__(self, expr, op='', val=''):
 self.expr = expr # can be another instance of Expression.
 self.op = op
 self.val = val

 def __str__(self):
 return ("%s %s %s" % (self.expr, self.op, self.val)).strip()

 def expand(self):
 self = Expr(self, choice('+-*/'), choice('12345'))

Then I tried messing around with Expr.__new__() and Expr.__init__() but that didn't work.

The only way I've got it to work is by doing the expanding part outside of the object, by a method of some other class. But I want the Expr class to be responsible for itself. How can I do this and why doesn't the above work?

+2 votes

It is difficult to install numpy package for my PC Windows 7, 64-bit OS. In the end, I install Enthought Canopy, which is recommended on line because it does install numpy automatically. Now, I can test it with

import numpy

it succeeds. On, it shows some interesting code example snippet, such as Cookbook / ParticleFilter, Markov chain etc.

I don't know how I can access these code examples, because I don't know where Enthought Canopy installs these package.

Could you help me on using numpy examples?

0 votes

I'm trying to figure out (or find an example) of polymorphism whereby I pass a commandline argument (a string) which comports to a class (in java, you would say that it comports to a given interface but I don't know if there is such a thing in Python) then that class of that name, somehow gets instantiated from that string. This way, I can have similar classes, but have my program use various ones by simply changing the commandline argument.

Can anyone show me how this might be done in Python?

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.