How do i instantiate a class_name passed via cmd line

Veek. M vek.m1234 at gmail.com
Sun Feb 14 00:39:40 EST 2016


Rick Johnson wrote:

> On Saturday, February 13, 2016 at 10:41:20 PM UTC-6, Veek. M wrote:
>> how do i replace the 'Ebay' bit with a variable so that I
>> can load any class via cmd line.
> 
> Is this what you're trying to do?
> 
> (Python2.x code)
>>>> import Tkinter as tk
>>>> classNames = ["Button", "Label"]
>>>> root = tk.Tk()
>>>> for className in classNames:
> classN = getattr(tk, className)
> instanceN = classN(root, text=className)
> instanceN.pack()
>     
> Note: You won't need to call "mainloop" when testing this
> code on the command line, only in a script or in the IDLE
> shell.
> 
>>>> root.mainloop()
Nope - this is what i'm doing:

class Foo():
 pass

x = 'Foo'

How do i use 'x' to create an instance of class Foo?




More information about the Python-list mailing list