Here it is listing attributes in th tkinter GUI module in the standard library

Here it is listing attributes in th tkinter gui

This preview shows page 67 - 70 out of 82 pages.

this file’s function. Here it is listing attributes in the tkinter GUI module in the standard library (a.k.a. Tkinter in Python 2.6): >>> import mydir >>> import tkinter >>> mydir.listing(tkinter) ------------------------------------------------------------ name: tkinter file: c:\PYTHON30\lib\tkinter\__init__.py ------------------------------------------------------------ 00) getdouble <class 'float'> 01) MULTIPLE multiple 02) mainloop <function mainloop at 0x02913B70> 03) Canvas <class 'tkinter.Canvas'> 04) AtSelLast <function AtSelLast at 0x028FA7C8> ...many more name omitted... 151) StringVar <class 'tkinter.StringVar'> Modules Are Objects: Metaprograms | 595 Copyright © ${Date}. ${Publisher}. All rights reserved.
Image of page 67
152) ARC arc 153) At <function At at 0x028FA738> 154) NSEW nsew 155) SCROLL scroll ------------------------------------------------------------ tkinter has 156 names ------------------------------------------------------------ We’ll meet getattr and its relatives again later. The point to notice here is that mydir is a program that lets you browse other programs. Because Python exposes its internals, you can process objects generically. Importing Modules by Name String The module name in an import or from statement is a hardcoded variable name. Some- times, though, your program will get the name of a module to be imported as a string at runtime (e.g., if a user selects a module name from within a GUI). Unfortunately, you can’t use import statements directly to load a module given its name as a string— Python expects a variable name, not a string. For instance: >>> import "string" File "<stdin>", line 1 import "string" ^ SyntaxError: invalid syntax It also won’t work to simply assign the string to a variable name: x = "string" import x Here, Python will try to import a file x.py , not the string module—the name in an import statement both becomes a variable assigned to the loaded module and identifies the external file literally. To get around this, you need to use special tools to load a module dynamically from a string that is generated at runtime. The most general approach is to construct an import statement as a string of Python code and pass it to the exec built-in function to run ( exec is a statement in Python 2.6, but it can be used exactly as shown here—the parentheses are simply ignored): >>> modname = "string" >>> exec("import " + modname) # Run a string of code >>> string # Imported in this namespace <module 'string' from 'c:\Python30\lib\string.py'> † Tools such as mydir.listing can be preloaded into the interactive namespace by importing them in the file referenced by the PYTHONSTARTUP environment variable. Because code in the startup file runs in the interactive namespace (module __main__ ), importing common tools in the startup file can save you some typing. See Appendix A for more details. 596 | Chapter 24: Advanced Module Topics Copyright © ${Date}. ${Publisher}. All rights reserved.
Image of page 68
The exec function (and its cousin for expressions, eval ) compiles a string of code and passes it to the Python interpreter to be executed. In Python, the byte code compiler is available at runtime, so you can write programs that construct and run other programs like this. By default, exec runs the code in the current scope, but you can get more specific by passing in optional namespace dictionaries.
Image of page 69
Image of page 70

You've reached the end of your free preview.

Want to read all 82 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture