Page 49 - Python Simple
P. 49

6. Mô-đun                                                        http://www.vithon.org/tutorial/2.5/node8.html



             Biến sys.path là một danh sách các chuỗi quyết định đường dẫn tìm kiếm các
             mô-đun của trình thông dịch. Nó được khởi tạo theo đường dẫn mặc định từ
             biến môi trường PYTHONPATH, hoặc từ một giá trị có sẵn nếu PYTHONPATH
             không được thiết lập. Bạn có thể sửa nó bằng cách dùng các công cụ trên danh
             sách:


                  >>> import sys
                  >>> sys.path.append('/ufs/guido/lib/python')


             6.3 dir() hàm



             Hàm có sẵn dir() được dùng để tìm các tên một mô-đun định nghĩa. Nó trả về
             một danh sách các chuỗi đã sắp xếp:

                  >>> import fibo, sys
                  >>> dir(fibo)
                  ['__name__', 'fib', 'fib2']
                  >>> dir(sys)
                  ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__',
                   '__stdin__', '__stdout__', '_getframe', 'api_version', 'argv',
                   'builtin_module_names', 'byteorder', 'callstats', 'copyright',
                   'displayhook', 'exc_clear', 'exc_info', 'exc_type', 'excepthook',
                   'exec_prefix', 'executable', 'exit', 'getdefaultencoding', 'getdlopenflags',
                   'getrecursionlimit', 'getrefcount', 'hexversion', 'maxint', 'maxunicode',
                   'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache',
                   'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags',
                   'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout',
                   'version', 'version_info', 'warnoptions']


             Không có thông số, dir() liệt kê các tên bạn đã định nghĩa:

                  >>> a = [1, 2, 3, 4, 5]
                  >>> import fibo
                  >>> fib = fibo.fib
                  >>> dir()
                  ['__builtins__', '__doc__', '__file__', '__name__', 'a', 'fib', 'fibo', 'sys']

             Lưu ý rằng nó liệt kê mọi loại tên: biến, mô-đun, hàm, v.v...


             dir() không liệt kê tên của các hàm và biến có sẵn. Nếu bạn muốn có danh
             sách của chúng, thì chúng được định nghĩa trong mô-đun chuẩn __builtin__:
                  >>> import __builtin__
                  >>> dir(__builtin__)
                  ['ArithmeticError', 'AssertionError', 'AttributeError', 'DeprecationWarning',
                   'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',
                   'FloatingPointError', 'FutureWarning', 'IOError', 'ImportError',
                   'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
                   'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented',
                   'NotImplementedError', 'OSError', 'OverflowError',




     5 of 10                                                                                  08/31/2011 10:26 AM
   44   45   46   47   48   49   50   51   52   53   54