2

Using python.....I have a list that contain names. I want to use each item in the list to create instances of a class. I can't use these items in their current condition (they're strings). Does anyone know how to do this in a loop.

class trap(movevariables):
    def __init__(self):
        movevariables.__init__(self)
        if self.X==0:
            self.X=input('Move Distance(mm) ')
        if self.Vmax==0:
            self.Vmax=input('Max Velocity? (mm/s)  ')
        if self.A==0:
            percentg=input('Acceleration as decimal percent of g'  )
            self.A=percentg*9806.65
        self.Xmin=((self.Vmax**2)/(2*self.A))
        self.calc()
    def calc(self):
        if (self.X/2)>self.Xmin:
            self.ta=2*((self.Vmax)/self.A)                # to reach maximum velocity, the move is a symetrical trapezoid and the (acceleration time*2) is used
            self.halfta=self.ta/2.                               # to calculate the total amount of time consumed by acceleration and deceleration
            self.xa=.5*self.A*(self.halfta)**2
        else:                                                               # If the move is not a trap, MaxV is not reached and the acceleration time is set to zero for subsequent calculations                                                        
            self.ta=0
        if (self.X/2)<self.Xmin:
            self.tva=(self.X/self.A)**.5
            self.halftva=self.tva/2
            self.Vtriang=self.A*self.halftva
        else:
            self.tva=0
        if (self.X/2)>self.Xmin:
            self.tvc=(self.X-2*self.Xmin)/(self.Vmax)  # calculate the Constant velocity time if you DO get to it
        else:
            self.tvc=0
        self.t=(self.ta+self.tva+self.tvc)
        print self

I'm a mechanical engineer. The trap class describes a motion profile that is common throughout the design of our machinery. There are many independent axes (trap classes) in our equipment so I need to distinguish between them by creating unique instances. The trap class inherits from movevariables many getter/setter functions structured as properties. In this way I can edit the variables by using the instance names. I'm thinking that I can initialize many machine axes at once by looping through the list instead of typing each one.

11
  • 1
    What do you need these strings to be? Need more specifics to even try to answer this. Commented Aug 28, 2009 at 13:31
  • The strings will be the new instance(s) for a class. I'm use to creating instances like this-----new=class(). I want to be able to get the 'new' out of a list. Commented Aug 28, 2009 at 13:58
  • Can you give us an example of the list? Commented Aug 28, 2009 at 14:12
  • 1
    @Tgatman, can you edit your question add those new details? what is class here? class is a keyword in python. What are you trying to achieve? Write all the details in the question to get the best help. Commented Aug 28, 2009 at 14:19
  • 1
    @Tgatman, can you post the class "trap" source code? Just edit the question and add the code. I'm trying to help. Commented Aug 28, 2009 at 14:36

5 Answers 5

2

You could use a dict, like:

classes = {"foo" : foo, "bar" : bar}

then you could do:

myvar = classes[somestring]()

this way you'll have to initialize and keep the dict, but will have control on which classes can be created.

Sign up to request clarification or add additional context in comments.

Comments

2

The getattr approach seems right, a bit more detail:

def forname(modname, classname):
    ''' Returns a class of "classname" from module "modname". '''
    module = __import__(modname)
    classobj = getattr(module, classname)
    return classobj

From a blog post by Ben Snider.

Comments

1

If it a list of classes in a string form you can:

classes = ['foo', 'bar']
for class in classes:
    obj = eval(class)

and to create an instance you simply do this:

instance = obj(arg1, arg2, arg3)

Comments

1

EDIT

If you want to create several instances of the class trap, here is what to do:

namelist=['lane1', 'lane2']
traps = dict((name, trap()) for name in namelist)

That will create a dictionary that maps each name to the instance.

Then to access each instance by name you do:

traps['lane1'].Vmax

Comments

0

you're probably looking for getattr.

Comments