コード例 #1
0
ファイル: importing_modules.py プロジェクト: arante/pyloc
def test():
    """Test cases."""

    print('Example 1:')
    fibo.fib(1000)

    print('Example 2:')
    print(fibo.fib1(1000))
    
    print('Example 3:')
    print(fibo.__name__)

    # Assigning function a local name
    fib = fibo.fib

    print('Example 4:')
    fib(1000)
コード例 #2
0
ファイル: 05-Modules.py プロジェクト: subhk/python-notebooks
        result.append(b)
        a, b = b, a+b
    return result

if __name__ == "__main__":
    import sys
    fib1(int(sys.argv[1]))

# + [markdown] {"slideshow": {"slide_type": "slide"}}
# You can use the function fib by importing fibo which is the name of the file without .py extension.

# + {"slideshow": {"slide_type": "fragment"}, "execution": {"iopub.status.busy": "2020-09-12T13:29:31.972627Z", "iopub.execute_input": "2020-09-12T13:29:31.973839Z", "iopub.status.idle": "2020-09-12T13:29:31.978991Z", "shell.execute_reply": "2020-09-12T13:29:31.978327Z"}}
import fibo
print(fibo.__name__)
print(fibo.__file__)
fibo.fib1(1000)

# + {"execution": {"iopub.status.busy": "2020-09-12T13:29:31.983810Z", "iopub.execute_input": "2020-09-12T13:29:31.985343Z", "iopub.status.idle": "2020-09-12T13:29:31.990318Z", "shell.execute_reply": "2020-09-12T13:29:31.989653Z"}}
%run fibo.py 1000

# + {"slideshow": {"slide_type": "slide"}, "execution": {"iopub.status.busy": "2020-09-12T13:29:31.995175Z", "iopub.execute_input": "2020-09-12T13:29:31.996128Z", "iopub.status.idle": "2020-09-12T13:29:31.998886Z", "shell.execute_reply": "2020-09-12T13:29:31.999551Z"}}
help(fibo)

# + [markdown] {"slideshow": {"slide_type": "slide"}}
# ## Executing modules as scripts
#
# When you run a Python module with
# ```bash
# $ python fibo.py <arguments>
# ```
# the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__". The following code will be executed only in this case and not when it is imported.