statements and dot expressions must be python variablenot stringmoreoverin an import the name is taken literally (not evaluated)and in dot syntax must evaluate to the object (not its string nameto be genericaddcomponents steps through list of name strings and relies on __import__ to import and return the module identified by each string in factthe for loop containing these statements works as though all of these statements were runimport demodlgdemoradiodemocheckdemoscale part demodlg demo(rootpart demoradio demo(rootpart democheck demo(rootpart demoscale demo(rootbut because the script uses list of name stringsit' easier to change the set of demos embedded--simply change the listnot the lines of executable code furthersuch datadriven code tends to be more compactless redundantand easier to debug and maintain incidentallymodules can also be imported from name strings by dynamically constructing and running import statementslike thisfor demo in demomodulesexec('from % import demodemopart eval('demo')(rootmake and run from fetch known import name by string the exec statement compiles and runs python statement string (herea from to load module' demo class)it works here as if the statement string were pasted into the source code where the exec statement appears the following achieves the same effect by running an import statement insteadfor demo in demomodulesexec('import %sdemopart eval(demodemo(rootmake and run an import fetch module variable by name too because it supports any sort of python statementthese exec/eval techniques are more general than the __import__ callbut can also be slowersince they must parse code strings before running them howeverthat slowness may not matter in guiusers tend to be significantly slower than parsers configuring at construction time one other alternative worth mentioningnotice how example - configures and repacks each attached demo frame for its role in this guidef addcomponents(root)for demo in demomodulesmodule __import__(demopart module demo(rootimport by name string attach an instance as we'll see later in this bookexec can also be dangerous if it is running code strings fetched from users or network connections that' not an issue for the hardcoded strings used internally in this example tkinter tourpart