When the interpreter reads a python script file, it does two things:

(1) set some special variable.

(2) it executes all the code from 1st line of that script file.

__name__ (2 underscores before and after) is a special python variable.

we can import this script as a module.

and also execute this script directly that the interpreter will assign the hard-coded string "__main__" to the __name__ variable

# foo.py
# I am using python 3.4

print ("Befor foo function.")
def foo():
print ("foo function.")
print ("After foo function.")
if __name__ == "__main__":
foo()

if we run it with

$ python foo.py

then the result is :

Befor foo function.
After foo function.
foo function.

and if we import it, then run $ python hello.py

# hello.py
import foo

the running result is:

Befor foo function.
After foo function.

Python, import, module的更多相关文章

  1. 测试linux python import module

    源码test.py #!/usr/bin/env python # -*- coding: UTF-8 -*- import os os.system("df -h") 运行结果( ...

  2. python import, from xx import yy

    区别: 用import modulexx/packagexx.moduleyy是导入某一模块,如果想引用模块的内容(class, method,variables...)必须用全名,即 [module ...

  3. Python: import vs from (module) import function(class) 的理解

    Python: Import vs From (module) import function(class) 本文涉及的 Python 基本概念: Module Class import from . ...

  4. python import 错误 TypeError: 'module' object is not callable

    python import 错误 TypeError: 'module' object is not callable 在这里,有 Person.py test.py; 在 test.py 里面 im ...

  5. python import eventlet包时提示ImportError: cannot import name eventlet

    root@zte-desktop:/home/ubuntu/python-threads# cat eventlet.py #!/usr/bin python import eventlet from ...

  6. Requests:Python HTTP Module学习笔记(一)(转)

    Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...

  7. python import

    在执行 import module 时 会从 1 当前目录 2 pythonpath(可以通过 os.sys.path 查看) 3 python 安装目录   b import 了 a, c impo ...

  8. Python import / pyd / dll

    使用Python import 模块时, 先会在模块的搜索path里依次搜索(前面会覆盖之后出现的同名模块),次序为: 1. 程序的主目录(交互模式下当前的工作目录或 脚本文件所在的目录) 2. 环境 ...

  9. Python import与from import使用及区别介绍

    Python程序可以调用一组基本的函数(即内建函数),比如print().input()和len()等函数.接下来通过本文给大家介绍Python import与from import使用及区别介绍,感 ...

随机推荐

  1. Thinkphp5——数据库表名的大小写问题

    ThinkPHP5中数据库的表名如果是驼峰命名法,会被转换成小写加下划线,解决方法如下: 1.表名全部小写,因为数据库的表名区分大小写的. 2.使用Db::table("表名"), ...

  2. Python中的Tcp协议应用之TCP服务端-线程版

    利用线程实现,一个服务端同时服务多个客户端的需求. TCP服务端-线程版代码实现: import socket import threading def handle_client_socket(ne ...

  3. syslog日志

    Syslog协议 系统日志(Syslog)协议是在一个IP网络中转发系统日志信息的标准,它是在美国加州大学伯克利软件分布研究中心(BSD)的TCP/IP系统实施中开发的,目前已成为工业标准协议,可用它 ...

  4. React-Native项目在Android真机上调试

    目录 1.确保你的设备已经成功连接.可以终端输入adb devices来查看: 2.终端运行npm start 开启本地服务,成功后运行react-native run-android来在设备上安装并 ...

  5. 数据库Oracle数字函数

    数字函数不多: ROUND(arg1):四舍五入保留整数. arg1:数字类型.原数字. arg2:整数类型.小数点保留的位数. SQL> select round(1256.564,2) fr ...

  6. CF1009F Dominant Indices(启发式合并)

    You are given a rooted undirected tree consisting of nn vertices. Vertex 11 is the root. Let's denot ...

  7. WOE(证据权重)为何这样计算?

    更多大数据分析.建模等内容请关注公众号<bigdatamodeling> 先简单回顾一下WOE的含义.假设x是类别变量或分箱处理过的连续变量,含R个类别或分段,取值为{C1, ..., C ...

  8. 【系列专题】ECMAScript 重温系列(10篇全)

    ES6 系列ECMAScript 2015 [ES]150-重温基础:ES6系列(一) [ES]151-重温基础:ES6系列(二) [ES]152-重温基础:ES6系列(三) [ES]153-重温基础 ...

  9. LNMP-Nginx配置SSL

    SLL工作流程: 浏览器发送一个https的请求给服务器: 服务器要有一套数字证书,可以自己制作(后面的操作就是阿铭自己制作的证书),也可以向组织申请,区别就是自己颁发的证书需要客户端验证通过,才可以 ...

  10. sql语句字符串包含

    select instr('1222','122') from dual//前者包含后者>0 oracle mysql 数据库可中 select charindex('1','12') from ...