【再回首Python之美】【模块】使用module
2018-02-07 14:27
435 查看
python自带了功能非富多彩的标准库,以及还有很多第三方库。
使用这些功能的基本方法:使用模块
可使用对象:模块中的变量和函数,由模块.py或者.pyc文件提供
模块存在形式:.py或者.pyc文件,一般在C:\Python27\Lib目录下
效率:通过使用python自带模块的函数或者变量,可以重用其代码,从而提高自己代码工作量,甚至代码性能
示例代码:
#ex_template.py by oneself
self_file = __file__#current file path
print "\nShow function and variable in random module=========="
import random #Tell python, use random module
print dir(random) #With the help of dir()
#会输出如下list
#['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random',
# 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill',
#'_BuiltinMethodType', '_MethodType', '__all__', '__builtins__',
#'__doc__', '__file__', '__name__', '__package__', '_acos',
#'_ceil', '_cos', '_e', '_exp', '_hashlib', '_hexlify',
#'_inst', '_log', '_pi', '_random', '_sin', '_sqrt',
#'_test', '_test_generator', '_urandom', '_warn',
#'betavariate', 'choice', 'division', 'expovariate',
#'gammavariate', 'gauss', 'getrandbits', 'getstate',
#'jumpahead', 'lognormvariate', 'normalvariate',
#'paretovariate', 'randint', 'random', 'randrange',
#'sample', 'seed', 'setstate', 'shuffle', 'triangular',
#'uniform', 'vonmisesvariate', 'weibullvariate']
print "\nUse function in random module======"
from random import randint #Use randint function in random module
print randint(0,10) #Return random integer in range [0, 10], including both end points.
print random.randint(0,10) #be equivalent to above two line code
print "\nUse variable in random module======"
from random import __file__ #Use __file__ variable in random module
print __file__ #C:\Python27\lib\random.pyc
print random.__file__ #be equivalent to above two line code
print "\nAlias for variable in random module======"
from random import __file__ as random_file
print random_file #C:\Python27\lib\random.pyc
print "\nAlias for function in random module======"
from random import randint as random_randint
print random_randint(0,10)
print "\nexit." + self_file编译执行:
(end)
使用这些功能的基本方法:使用模块
可使用对象:模块中的变量和函数,由模块.py或者.pyc文件提供
模块存在形式:.py或者.pyc文件,一般在C:\Python27\Lib目录下
效率:通过使用python自带模块的函数或者变量,可以重用其代码,从而提高自己代码工作量,甚至代码性能
示例代码:
#ex_template.py by oneself
self_file = __file__#current file path
print "\nShow function and variable in random module=========="
import random #Tell python, use random module
print dir(random) #With the help of dir()
#会输出如下list
#['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random',
# 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill',
#'_BuiltinMethodType', '_MethodType', '__all__', '__builtins__',
#'__doc__', '__file__', '__name__', '__package__', '_acos',
#'_ceil', '_cos', '_e', '_exp', '_hashlib', '_hexlify',
#'_inst', '_log', '_pi', '_random', '_sin', '_sqrt',
#'_test', '_test_generator', '_urandom', '_warn',
#'betavariate', 'choice', 'division', 'expovariate',
#'gammavariate', 'gauss', 'getrandbits', 'getstate',
#'jumpahead', 'lognormvariate', 'normalvariate',
#'paretovariate', 'randint', 'random', 'randrange',
#'sample', 'seed', 'setstate', 'shuffle', 'triangular',
#'uniform', 'vonmisesvariate', 'weibullvariate']
print "\nUse function in random module======"
from random import randint #Use randint function in random module
print randint(0,10) #Return random integer in range [0, 10], including both end points.
print random.randint(0,10) #be equivalent to above two line code
print "\nUse variable in random module======"
from random import __file__ #Use __file__ variable in random module
print __file__ #C:\Python27\lib\random.pyc
print random.__file__ #be equivalent to above two line code
print "\nAlias for variable in random module======"
from random import __file__ as random_file
print random_file #C:\Python27\lib\random.pyc
print "\nAlias for function in random module======"
from random import randint as random_randint
print random_randint(0,10)
print "\nexit." + self_file编译执行:
(end)
相关文章推荐
- 【再回首Python之美】【模块-random】random模块的基本使用
- Python的模块module导入与使用
- ubuntu下使用pip3.6安装模块总是报ssl module in Python is not available的错误
- [Python module]使用threading模块实现多线程编程一[综述]
- python~module gzip 使用方法,及简介! 附件 python ~ os 模块!!!
- Python使用模块Pyserial模块报<ImportError:No module named tools.list_ports>
- 【Python学习】python不要使用任何模块名作为文件名(module 'xgboost' has no attribute 'DMatrix')
- 【再回首Python之美】【模块 math】math模块的基本使用
- 【再回首Python之美】【url/json小酌】查天气(使用urllib2,json模块)
- 使用Visual Studio 6.0 (C/C++)开发Python扩展模块
- PYTHON 模块中使用了traceback 模块报错时的解决方法
- 使用 Boost.Python 嵌入 Python 模块到 C++
- Python的内存泄漏及gc模块的使用
- 关于使用HttpModule模块处理登录验证示例
- Python体验(02)-模块Module
- Python模块之chardir: 使用chartdir生成各种图表
- FLEX使用模块MODULE
- Python 学习笔记 - 11.模块(Module)
- 在Python 中使用模块和类
- [有趣的Python 8]使用C模块来提高性能 [转]