一、用于编译pc文件为pyc文件
import py_compile
py_compile.compile(‘hello.py’)
#import py_compile
#py_compile.compile('hello.py') 是用来编译py为pyc文件
python下编译py成pyc和pyo
其实很简单,
用
python -m py_compile file.py
python -m py_compile /root/src/{file1,file2}.py
编译成pyc文件。
也可以写份脚本来做这事:
Code:
import py_compile
py_compile.compile('path') //path是包括.py文件名的路径
二、python变量
----------------------变量命名---------------------------
# 下面的两个i并不是同一个对象
i = 1
print id(i)
i = 2
print id(i)
# 正确的变量命名
var_1 = 1
print var_1
_var1 = 2
print _var1
# 错误的变量命名
1_var = 3
print 1_var
$var = 4
print $var
----------------------变量赋值---------------------------
# 一次新的赋值操作,将创建一个新的变量
x = 1
print id(x)
x = 2
print id(x)
#print y
# 给多个变量赋值
a = (1, 2, 3)
(x, y, z) = a
print "x =", x
print "y =", y
print "z =", z
----------------------局部变量---------------------------
# 局部变量
def fun():
local = 1
print local
fun()
----------------------全局变量---------------------------
#gl.py
# 全局变量
global _a
_a = 1
_b = 2
#global_variable.py
# 在文件的开头定义全局变量
_a = 1
_b = 2
def add():
global _a
_a = 3
return "_a + _b =", _a + _b
def sub():
global _b
_b = 4
return "_a - _b =", _a - _b
print add()
print sub()
# 错误的使用全局变量
_a = 1
_b = 2
def add():
_a = 3
return "_a + _b =", _a + _b
def sub():
_b = 4
return "_a - _b =", _a - _b
print add()
print sub()
#use_global.py
#调用全局变量
import gl
def fun():
print gl._a
print gl._b
fun()
----------------------变量命名---------------------------
#const.py
class _const:
class ConstError(TypeError): pass
def __setattr__(self,name,value):
if self.__dict__.has_key(name):
raise self.ConstError, "Can't rebind const(%s)"%name
self.__dict__[name]=value
import sys
sys.modules[__name__]=_const()
#use_const.py
import const
const.magic = 23
const.magic = 33
----------------------变量类型---------------------------
# 下面的两个i并不是同一个对象
i = 1
print id(i)
i = 2
print id(i)
# 整型
i = 1
print type(i)
# 长整型
l = 9999999990
print type(l)
# 浮点型
f = 1.2
print type(f)
# 布尔型
b = True
print type(b)
# 复数类型
c = 7 + 8j
print type(c)
-------------------单引号和双引号的使用--------------------
# 单引号和双引号的使用是等价
str = "hello world!"
print str
str = 'hello world!'
print str
# 三引号的用法
str = '''he say "hello world!"'''
print str
# 三引号制作doc文档
class Hello:
'''hello class'''
def printHello():
'''print hello world'''
print "hello world!"
print Hello.__doc__
print Hello.printHello.__doc__
# 转义字符
str = 'he say:\'hello world!\''
print str
# 直接输出特殊字符
str = "he say:'hello world!'"
print str
str = '''he say:'hello world!' '''
print str
----------------------算数表达式---------------------------
# 算术表达式
from __future__ import division
print "1 + 1 =", 1 + 1
print "2 - 1 =", 2 - 1
print "2 * 3 =", 2 * 3
print "4 / 2 =", 4 / 2
print "1 / 2 =", 1 / 2
print "1 / 2 =", 1.0 / 2.0
print "3 % 2 =", 3 % 2
print "2 ** 3 =", 2 ** 3
# 算术运算的优先级
a = 1
b = 2
c = 3
d = 4
print "a + b * c % d =", a + b * c % d
print "(a + b) * (c % d) =", (a + b) * (c % d)
# 关系表达式
print 2 > 1
print 1 <= 2
print 1 == 2
print 1 != 2
print 1 <> 2
# 关系表达式的优先级别
print "1 + 2 < 3 - 1 =>", 1 + 2, "<", 3 - 1, "=>", 1 + 2 < 3 - 1
print "1 + 2 <= 3 > 5 % 2 =>", 1 + 2, "<=", 3, ">", 5 % 2, "=>", 1 + 2 <= 3 > 5 % 2
# 逻辑运算符
print not True
print False and True
print True and False
print True or False
# 逻辑表达式的优先级别
print "not 1 and 0 =>", not 1 and 0
print "not (1 and 0) =>", not (1 and 0)
print "(1 <= 2) and False or True =>", (1 <= 2) and False or True
print "(1 <= 2) or 1 > 1 + 2 =>", 1 <= 2, "or", 1 > 2, "=>", (1 <= 2) or (1 < 2)