Python Module

1 如何实现一个module:

简单的说写个函数保存为xx.py 然后再另一个py文件中import就可以了。

import的语法为:

import module1[, module2[,... moduleN]

A module is loaded only once, regardless of the number of times it is
imported. This prevents the module execution from happening over and
over again if multiple imports
occur.

2 from xx import XX

Syntax:

from modname import name1[, name2[, ... nameN]]

Example:

For example, to import the function fibonacci from the module fib, use the following statement:

from fib import fibonacci


3.python 搜索module的方法:
  • The current directory.

  • If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.

  • If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/.

4.

The dir( ) Function:

The dir() built-in function returns a sorted list of strings containing the names defined by a module.

The list contains the names of all the modules, variables, and functions that are defined in a module.

Example:

#!/usr/bin/python

# Import built-in module math
import math

content = dir(math)

print content;

This would produce following result:

['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 
'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp',
'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh',
'sqrt', 'tan', 'tanh']

Here the special string variable __name__ is the module's name, and __file__ is the filename from which the module was loaded.

The globals() and locals() Functions:

The globals() and locals() functions can be used to return the names in the global and local namespaces depending on the location from where they are called.

If locals() is called from within a function, it will return all the names that can be accessed locally from that function.

If globals() is called from within a function, it will return all the names that can be accessed globally from that function.

The return type of both these functions is dictionary. Therefore, names can be extracted using the keys() function.

The reload() Function:

When the module is imported into a script, the code in the top-level portion of a module is executed only once.

Therefore, if you want to reexecute the top-level code in a module, you can use the reload() function. The reload() function imports a previously imported module again.

Syntax:

The syntax of the reload() function is this:

reload(module_name)

Here module_name is the name of the module you want to reload and not the string containing the module name. For example to re-load hello module, do the following:

reload(hello)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值