How do i instantiate a class_name passed via cmd line

Rick Johnson rantingrickjohnson at
Sun Feb 14 00:55:14 EST 2016

On Saturday, February 13, 2016 at 11:39:56 PM UTC-6, Veek. M wrote:
> Nope - this is what i'm doing:
> class Foo():
>  pass
> x = 'Foo'
> How do i use 'x' to create an instance of class Foo?

Use the builtin function `getattr` on the module that contains
the class named "Foo". 

For example:


is equivelant to:

    getattr(module, "Foo")

Here is an interactive session:

    py> import Tkinter
    py> Tkinter.Label
    <class Tkinter.Label at 0x023BE5A8>
    py> getattr(Tkinter, "Label")
    <class Tkinter.Label at 0x023BE5A8>

More information about the Python-list mailing list