zoukankan      html  css  js  c++  java
  • Python 模块引入,脚本执行

    引入模块

    创建一个fibo.py

    def fib(n):    # write Fibonacci series up to n
        a, b = 0, 1
        while b < n:
            print b
            a, b = b, a+b
    

    引入这个文件,就可以使用了

    >>> import fibo
    >>> fibo.fib(100)
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    

    或者

    >>> from fibo import fib
    >>> fib(100)
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    
    

    或者

    >>> from fibo import *
    >>> fib(10)
    1
    1
    2
    3
    5
    8
    
    

    作为脚本执行

    fibo.py

    def fib(n):
        a,b = 0,1
        while b < n:
            print b
            a,b = b,a+b
    
    if __name__ == "__main__":
        import sys
        fib(int(sys.argv[1]))
    
    
    # python fibo.py 50
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    

    dir() 函数

    >>> import fibo
    >>> dir(fibo)
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'fib']
    >>> import sys
    >>> dir(sys)
    ['__displayhook__', '__doc__', '__egginsert', '__excepthook__', '__name__', '__package__', '__plen', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', '_multiarch', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'last_traceback', 'last_type', 'last_value', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'pydebug', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions']
    
    
  • 相关阅读:
    sql语句
    CSS笔记
    mysql与oracle区别
    ajax请求aspx
    列表简单排序
    列表删除元素
    正则表达式简介
    创建列表
    动态串 StringBuilder
    StrinBuilder 追加 ,插入,删除,和替换操作
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/9938895.html
Copyright © 2011-2022 走看看