May 10th Thursday (五月 十日 木曜日)

本文探讨了Python中模块的全局变量使用及避免命名冲突的方法。作者解释了每个模块都有其独立的全局符号表,这使得模块内的全局变量不会与用户的全局变量发生意外冲突。此外,还介绍了如何通过特定的语法访问模块的全局变量,以及模块之间的导入机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Each module has its own private symbol table, which is used as the global symbol table by
all functions defined in the module. Thus, the author of a module can use global variables
in the module without worrying about accidental clashes with a user's global variables.  On
the other hand, if you know what you are doing you can touch a module's global variables with
the same notation used to refer to its functions, modname.itemname.

  Modules can import other modules. It is customary but not required to place all import
statements at the beginning of a module (or script, for that matter). The imported module names
are placed in the importing module's global symbol table.

  When a module named spam is imported, the interpreter searches for a file named spam.py in
the current directory, and then in the list of directories specified by the environment variable
PYTHONPATH. This has the same syntax as the shell variable PATH, that is, a list of directory names.
When PYTHONPATH is not set, or when the file is not found there, the search continues in an installation
-dependent default path; on Unix, this is usually .:/usr/local/lib/python.

  Actually, modules are searched in the list of directories given by the variable sys.path which is
initialized from the directory containing the input script (or the current directory), PYTHONPATH and
the installation-dependent default.  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值