module+standard library.py

#导入模块
import sys
sys.path
sys.path.append(‘D:\program files\Python34\PyWorks‘)    #hello.py文件路径
            #不用append PyWorks路径也可以,因为D:\program files\Python34在sys.path中
import hello    #第一次导入会执行,路径增加.pyc文件
import hello    #第二次不会执行
#执行第二次的方法(假如hello修改后需要再重新导入)
import imp
imp.reload(hello)
‘‘‘
#从模块导入函数:
import somemodule
from somemodule import somefunction
from sommodule import function1,function2,functoin3
from somemodule import *
import somemodule as module
from somemodule import somefunction as functoin
‘‘‘

print(‘__name__ =‘,__name__)    #__name__ = __main__
print(‘hello.__name__ =‘,hello.__name__)    #hello.__name__ = hello
hello.hello()

#不修改sys.path导入模块的方法
#1,将模块放置在合适的位置
import pprint   #如果数据结构过大,不能在一行打印完,可以用pprint
pprint.pprint(sys.path)
#解释器可以从这些目录中找到所需的模块
#site-packages目录是最佳选择,因为它就是用来做这些事的

#2,告诉解释器去哪里找
#不希望将自己的模块填满Python解释器目录
#没有在Python解释器目录中存储文件的权限
#想将模块放在其他地方
#编辑sys.path,这不是通用的方法。标准方法是在PYTHONPATH环境变量中包含模块所在目录

#包
#当模块存储在文件中时(扩展名为.py),包就是模块所在的目录
#为了让Python将其作为包对待,他必须包含一个名为__init__py的文件(模块)
import PyWorks  #只有__init__模块可用,hello,if_test不可用
import PyWorks.hello    #hello模块可用,但只能通过全名PyWorks.hello 来使用
from PyWorks import if_test     #if_test模块可用,可通过短名if_test使用

#dir函数
#查看模块包含的内容,所有函数,类,变量
print(dir(hello))
print([n for n in dir(hello) if not n.startswith(‘_‘)])

#__all__
#在模块内部设置的。from module import * 时可以屏蔽不想要的变量,函数,类
#还是可以通过from module import function 或module.function访问
#在模块内部被设置。
__all__=[‘a‘,‘b‘,‘c‘]

#__doc__,help(module)
#查看模块帮助

#__file__
#查看模块文件的位置

#标准库
#sys,os,fileinput,set,heap,deque,time,random
#fileinput
#>>>python some_script.py file1.txt file2.txt file3.txt
#依次对fili1到file3文件中所有行进行遍历。
#函数file.input,filename,lineno,filelineno,isfirstline,isstdin,nextfile,close

#shelve P188
#在文件中存储数据,当做字典用
import shelve
db=shelve.open(r‘F:\test.dat‘,writeback=True)  #shelve创建一定的是.dat文件
person={}
person[‘name‘]=[‘a‘,‘b‘]
person[‘age‘]=[1,2,3]
db[‘1‘]=person
db[‘2‘]=person
print(db[‘1‘][‘name‘])
db[‘1‘][‘name‘].append(‘c‘) #这句不会写入shelve
print(db[‘1‘][‘name‘])
tmp=db[‘1‘][‘name‘]
tmp.append(‘c‘)
print(tmp)
db[‘1‘][‘name‘]=tmp
print(db[‘1‘][‘name‘])
db.close()

#re
#参见regular文件夹
时间: 2025-01-12 05:39:55

module+standard library.py的相关文章

Python入门之面向对象module,library,package之间区别

背景 Python中有一些基本的名词,很多人,尤其是一些初学者,可能听着就很晕. 此处,简单总结一下,module,library,package之间的大概区别. Python中的module的简介 module,中文翻译为:模块 Python中的module,说白了,就是Python文件,而python文件一般后缀为py,所以就是你的xxx.py而已. library简介 library,中文翻译为:库,也常称为:库文件 之所以此处不说是Python中的library,那是因为,本身libra

boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

直接代码: 代码段1: 1 #include <iostream> 2 #include <string> 3 #include <boost/bind/bind.hpp> 4 5 class some_class 6 { 7 public: 8 typedef void result_type; 9 void print_string(const std::string& s) const 10 { std::cout << s <<

boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

代码段1: 1 #include <boost/function.hpp> 2 #include <iostream> 3 4 5 float mul_ints(int x, int y) { return ((float)x) * y; } 6 struct int_div { 7 float operator()(int x, int y) const { return ((float)x)/y; }; 8 }; 9 10 int main() 11 { 12 boost::f

[RK_2014_1024][C++_01]C++ Standard Library

1 Standard headers 1.1 Containers 1.2 General 1.3 Localization 1.4 Strings 1.5 Streams and Input/Output 1.6 Language support 1.7 Thread support library 1.8 Numerics library 1.9 C standard library 2.本文网址[tom-and-jerry发布于2014-10-24 14:23] http://www.cn

Beyond the C++ Standard Library中文版pdf

下载地址:网盘下载 内容简介  · · · · · · Introducing the Boost libraries: the next breakthrough in C++ programming Boost takes you far beyond the C++ Standard Library, making C++ programming more elegant, robust, and productive. Now, for the first time, a leading

C++学习书籍推荐《The C++ Standard Library 2nd》下载

百度云及其他网盘下载地址:点我 编辑推荐 经典C++教程十年新版再现,众多C++高手和读者好评如潮 畅销全球.经久不衰的C++ STL鸿篇巨著 C++程序员案头必 备的STL参考手册 全面涵盖C++11新标准 名人推荐 在C++的著作当中,这本书的地位是无可替代的.要成为合格的C++开发者,就必须掌握C++标准库,而要掌握C++标准库,这本书可以说是不二法门.这本书最了不起的地方,就在于面对庞大复杂的C++标准库,能够抽丝剥茧,化难为易,引导读者循序渐进,深入浅出地掌握C++标准库. --孟岩 

C++, standard library

0. 1. Standard library header <algorithm> #include <algorithm> // for sort std::array<int,10> s = {5,7,2,8,6,1,9,0,3}; // initial an array std::sort(s.begin(), s.end()); // sort the array 原文地址:https://www.cnblogs.com/sarah-zhang/p/122937

Publish .net standard library with all it&#39;s dependencies?

Publish .net standard library with all it's dependencies? 回答1 At the time of writing, it looks like it's by design and there's quite some fuss and confusion about it, see logged issue on GitHub. Moreover, when publishing a NuGet package for project A

Python Tutorial学习(十一)-- Brief Tour of the Standard Library – Part II

11.1. Output Formatting 格式化输出 The repr module provides a version of repr() customized for abbreviated displays of large or deeply nested containers: >>> import repr >>> repr.repr(set('supercalifragilisticexpialidocious')) "set(['a',