标签:python
1.查询模块:按目录依次查找需要导入的模块,模块目录一般在:/usr/lib64/python2.7In [2]: sys.path Out[2]: ['', '/usr/bin', '/usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/python_memcached-1.58-py2.7.egg', '/usr/lib/python2.7/site-packages/IPython/extensions', '/root/.ipython']
2.自定义模块目录
方法一:sys.path.append(),一般加在目录列表最后
In [3]: sys.path.append("/root/python/")
In [4]: sys.path
Out[4]:
['',
'/usr/bin',
'/usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg',
'/usr/lib64/python27.zip',
'/usr/lib64/python2.7',
'/usr/lib64/python2.7/plat-linux2',
'/usr/lib64/python2.7/lib-tk',
'/usr/lib64/python2.7/lib-old',
'/usr/lib64/python2.7/lib-dynload',
'/usr/lib64/python2.7/site-packages',
'/usr/lib/python2.7/site-packages',
'/usr/lib/python2.7/site-packages/python_memcached-1.58-py2.7.egg',
'/usr/lib/python2.7/site-packages/IPython/extensions',
'/root/.ipython',
'/root/python/']方法二:修改环境变量,一般加在目录列表前面
vim /root/.bashrc # 加入 export PYTHONPATH=/root/python source /root/.bashrc # 刷新
例子:统计一个文件,行数、单词数、字符数(和wc命令相同效果)
说明:为了避免使用split切割之后,最后多出一个空字符串,而使用count()
#/usr/bin/env python
def count(s):
char = len(s)
words = len(s.split())
lines = s.count("\n")
print lines,words,char
file1 = open("/etc/passwd","r")
s = file1.read()
count(s)3.脚本形式,导入模块,脚本名字不能是数字,会产生一个编译文件
例子:
#!/usr/bin/env python import wc
说明:目录下生产编译文件:wc.pyc
4.py和wc.py的__name__内置变量不一样,前者是wc,或者是__main__,修改wc.py,执行自己时,输出自己的结果,被调用时,执行不显示源结果:
wc.py:
#/usr/bin/env python
def count(s):
char = len(s)
words = len(s.split())
lines = s.count("\n")
print lines,words,char
if __name__ == "__main__":
file1 = open("/etc/passwd","r")
s = file1.read()
count(s)test.py:
#!/usr/bin/env python
import wc
s = open("/root/python/10.py","r").read()
wc.count(s)5.包的形式,导入模块
四种导入方法:在包目录dir下创建一个__init__.py空文件
方法一:
from dir import wc
wc.count("abc")方法二:
import dir.wc
dir.wc.count("abc")方法三:
from dir.wc import count
count("abc")方法四:别名
from dir.wc import count as count
count("abc")6.面向对象编程:python、java、C++;面向过程编程:C、函数式编程、shell
类的(静态)属性:(人类的五官,理解为变量)
类的(动态)方法:(人类吃穿住行,理解为一个函数)
对象:类的实例化,之后才能有属性和方法
7.类的创建
类的方法中,至少有一个参数self
调用属性时,不带括号
调用方法时,使用括号;方法调用属性时,至少有一个self参数
属性调用其他方法:类名.属性名
例子:
class People():
color = "yellow"
def think(self):
self.color = "black"
print ("My color is %s "% (self.color))
ren = People() # 类的实例化
print ren.color # 类的属性外部调用
ren.think() # 类的方法外部调用,如加上print,则多一个默认return值none运行结果:
yellow
My color is black
8.私有属性在定义的类中的内部函数中被调用
例子:
class People():
color = "yellow"
__age = 27
def think(self):
self.color = "black"
print self.__age # 内部函数调用类的私有属性,外部函数不能直接调用
print ("My color is %s "% (self.color))
ren = People()
print ren.color
ren.think()9.外部调用私有属性(格式:实例化名._类名属性名),一般只是测试用
例子:
class People():
color = "yellow"
__age = 27
def think(self):
self.color = "black"
print self.__age
print ("My color is %s "% (self.color))
ren = People()
print ren.color
ren.think()
print ren._People__age # 外部调用私有属性10.类的方法
公有方法:内部和外部都可以调用
私有方法:内部函数调用
动态方法:classmethod()函数处理,没有被调用的类的其他参数不会加载进内存中
静态方法:
方法的定义和函数一样,但是需要把self作为第一个参数,如果还是有其他参数,继续加上;类实例化之后,采用“类名.方法名()”调用
例子1:私有方法调用
class People():
color = "yellow"
__age = 27
def __think(self):
self.color = "black"
print self.__age
print ("My color is %s "% (self.color))
def test(self):
self.__think() # 类的私有方法调用
ren = People()
ren.test() # 类的私有方法调用例子2:动态方法调用
class People():
color = "yellow"
__age = 27
def __think(self):
self.color = "black"
print self.__age
print ("My color is %s "% (self.color))
def test(self):
print ("Testing...")
cm = classmethod(test) # 动态方法定义
ren = People()
ren.cm() # 动态方法调用例子3:静态方法调用:
类函数不带self参数,该函数使用staticmethod()函数处理(如果不处理,缺少self,,调用时会报错),加载关于这个类的所有东西
class People():
color = "yellow"
__age = 27
def __think(self):
self.color = "black"
print self.__age
print ("My color is %s "% (self.color))
def test(): # 内部函数,不带self
print ("Testing...")
#print People.color # 因为没有self,不能调用该类的属性
cm = staticmethod(test) # 静态方法定义
ren = People()
ren.cm() # 静态方法调用例子4:加装饰器,只对下面的一个函数起作用,就可以使用类的方法调用了
class People():
color = "yellow"
__age = 27
def __think(self):
self.color = "black"
print self.__age
print ("My color is %s "% (self.color))
@classmethod # 加装饰器
def test(self): # 带self
print ("Testing...")
@staticmethod # 加装饰器
def test1(): # 不带self
print ("Testing1..")
ren = People()
People.test() # 类的方法调用
People.test1() # 类的方法调用标签:python
原文地址:http://blog.51cto.com/huangzp/2047412