常量和变量
局部变量与全局变量
global 声明全局变量
#!/usr/bin/python
def func1():
print "func1: local x is", x
def func2():
x = 2
print 'func2: local x is', x
def func3():
global x
print "func3: before change, x is", x
x = 2
print 'func3: changed x to', x
x = 1
print 'Global x is', x
func1()
print 'Global x is', x
func2()
print 'Global x is', x
func3()
print 'Global x is', x
def func1():
print "func1: local x is", x
def func2():
x = 2
print 'func2: local x is', x
def func3():
global x
print "func3: before change, x is", x
x = 2
print 'func3: changed x to', x
x = 1
print 'Global x is', x
func1()
print 'Global x is', x
func2()
print 'Global x is', x
func3()
print 'Global x is', x
复杂类型
控制语句
if 语句
if ... elif ... else , 示例:(注意冒号和缩进)
#!/usr/bin/python
# Filename : if.py
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.' # new block starts here
print "(but you don't win any prizes!)" # new block ends here
elif guess < number:
print 'No, it is a little higher than that.' # another block
# You can do whatever you want in a block ...
else:
print 'No, it is a little lower than that.'
# you must have guess > number to reach here
print 'Done'
# This last statement is always executed, after the if statement
# is executed.
# Filename : if.py
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.' # new block starts here
print "(but you don't win any prizes!)" # new block ends here
elif guess < number:
print 'No, it is a little higher than that.' # another block
# You can do whatever you want in a block ...
else:
print 'No, it is a little lower than that.'
# you must have guess > number to reach here
print 'Done'
# This last statement is always executed, after the if statement
# is executed.
while 循环语句
while ... [else ...] ,示例:(else 可选)
#!/usr/bin/python
# Filename : while.py
number = 23
stop = False
while not stop:
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.'
stop = True # This causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that.'
else: # you must have guess > number to reach here
print 'No, it is a little lower than that.'
else:
print 'The while loop is over.'
print 'I can do whatever I want here.'
print 'Done.'
# Filename : while.py
number = 23
stop = False
while not stop:
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.'
stop = True # This causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that.'
else: # you must have guess > number to reach here
print 'No, it is a little lower than that.'
else:
print 'The while loop is over.'
print 'I can do whatever I want here.'
print 'Done.'
内置函数和对象
函数
数学/逻辑/算法
其他
__import__(name, globals, locals, fromlist) -> module : 动态加载模块
def importName(modulename, name):
""" Import name dynamically from module
Used to do dynamic import of modules and names that you know their
names only in runtime.
Any error raised here must be handled by the caller.
@param modulename: full qualified mudule name, e.g. x.y.z
@param name: name to import from modulename
@rtype: any object
@return: name from module
"""
module = __import__(modulename, globals(), {}, [name])
return getattr(module, name)
""" Import name dynamically from module
Used to do dynamic import of modules and names that you know their
names only in runtime.
Any error raised here must be handled by the caller.
@param modulename: full qualified mudule name, e.g. x.y.z
@param name: name to import from modulename
@rtype: any object
@return: name from module
"""
module = __import__(modulename, globals(), {}, [name])
return getattr(module, name)
输入和输出
pickle 和 cPickle
cPickle 示例
#!/usr/bin/python
# Filename: pickling.py
import cPickle
shoplistfile = 'shoplist.data' # The name of the file we will use
shoplist = ['apple', 'mango', 'carrot']
# Write to the storage
f = file(shoplistfile, 'w')
cPickle.dump(shoplist, f) # dump the data to the file
f.close()
del shoplist # Remove shoplist
# Read back from storage
f = file(shoplistfile)
storedlist = cPickle.load(f)
print storedlist
# Filename: pickling.py
import cPickle
shoplistfile = 'shoplist.data' # The name of the file we will use
shoplist = ['apple', 'mango', 'carrot']
# Write to the storage
f = file(shoplistfile, 'w')
cPickle.dump(shoplist, f) # dump the data to the file
f.close()
del shoplist # Remove shoplist
# Read back from storage
f = file(shoplistfile)
storedlist = cPickle.load(f)
print storedlist
面向对象:类的编程
class 变量和 object 变量
示例
代码
#!/usr/bin/python
# Filename: objvar.py
class Person:
'''Represents a person.'''
population = 0
def __init__(self, name):
'''Initializes the person.'''
self.name = name
print '(Initializing %s)' % self.name
# When this person is created,
# he/she adds to the population
Person.population += 1
def sayHi(self):
'''Greets the other person.
Really, that's all it does.'''
print 'Hi, my name is %s.' % self.name
def howMany(self):
'''Prints the current population.'''
# There will always be atleast one person
if Person.population == 1:
print 'I am the only person here.'
else:
print 'We have %s persons here.' % \
Person.population
swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
swaroop.sayHi()
swaroop.howMany()
# Filename: objvar.py
class Person:
'''Represents a person.'''
population = 0
def __init__(self, name):
'''Initializes the person.'''
self.name = name
print '(Initializing %s)' % self.name
# When this person is created,
# he/she adds to the population
Person.population += 1
def sayHi(self):
'''Greets the other person.
Really, that's all it does.'''
print 'Hi, my name is %s.' % self.name
def howMany(self):
'''Prints the current population.'''
# There will always be atleast one person
if Person.population == 1:
print 'I am the only person here.'
else:
print 'We have %s persons here.' % \
Person.population
swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
swaroop.sayHi()
swaroop.howMany()
类的继承
示例
# Filename: inheritance.py
class SchoolMember:
'''Represents any school member.'''
def __init__(self, name, age):
self.name = name
self.age = age
print '(Initialized SchoolMember: %s)' % self.name
def tell(self):
print 'Name:"%s" Age:"%s" ' % (self.name, self.age),
class Teacher(SchoolMember):
'''Represents a teacher.'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print '(Initialized Teacher: %s)' % self.name
def tell(self):
SchoolMember.tell(self)
print 'Salary:"%d"' % self.salary
class Student(SchoolMember):
'''Represents a student.'''
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print '(Initialized Student: %s)' % self.name
def tell(self):
SchoolMember.tell(self)
print 'Marks:"%d"' % self.marks
t = Teacher('Mrs. Abraham', 40, 30000)
s = Student('Swaroop', 21, 75)
print # prints a blank line
members = [t, s]
for member in members:
member.tell()
# Works for instances of Student as well as Teacher
class SchoolMember:
'''Represents any school member.'''
def __init__(self, name, age):
self.name = name
self.age = age
print '(Initialized SchoolMember: %s)' % self.name
def tell(self):
print 'Name:"%s" Age:"%s" ' % (self.name, self.age),
class Teacher(SchoolMember):
'''Represents a teacher.'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print '(Initialized Teacher: %s)' % self.name
def tell(self):
SchoolMember.tell(self)
print 'Salary:"%d"' % self.salary
class Student(SchoolMember):
'''Represents a student.'''
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print '(Initialized Student: %s)' % self.name
def tell(self):
SchoolMember.tell(self)
print 'Marks:"%d"' % self.marks
t = Teacher('Mrs. Abraham', 40, 30000)
s = Student('Swaroop', 21, 75)
print # prints a blank line
members = [t, s]
for member in members:
member.tell()
# Works for instances of Student as well as Teacher
异常处理
Raising Exceptions
产生异常和捕获异常
try:
s = raw_input('Enter something --> ')
if len(s) < 3:
raise ShortInputException(len(s), 3)
# Other work can go as usual here.
except EOFError:
print '\nWhy did you do an EOF on me?'
except ShortInputException, x:
print '\nThe input was of length %d, it should be at least %d'\
% (x.length, x.atleast)
else:
print 'No exception was raised.'
s = raw_input('Enter something --> ')
if len(s) < 3:
raise ShortInputException(len(s), 3)
# Other work can go as usual here.
except EOFError:
print '\nWhy did you do an EOF on me?'
except ShortInputException, x:
print '\nThe input was of length %d, it should be at least %d'\
% (x.length, x.atleast)
else:
print 'No exception was raised.'
模组和包
Python 函数库
re
正则表达式语法
(?P<name>pattern) : 用名称指代匹配
>>> re.match('(?P<p>.*?)(?::\s*)(?P<msg>.*)', 'prompt: enter your name').group('p')
'prompt'
>>> re.match('(?P<p>.*?)(?::\s*)(?P<msg>.*)', 'prompt: enter your name').group('msg')
'enter your name'
>>> re.match('(?P<p>.*?)(?::\s*)(?P<msg>.*)', 'prompt: enter your name').group(0)
'prompt: enter your name'
>>> re.match('(?P<p>.*?)(?::\s*)(?P<msg>.*)', 'prompt: enter your name').group(1)
'prompt'
>>> re.match('(?P<p>.*?)(?::\s*)(?P<msg>.*)', 'prompt: enter your name').group(2)
'enter your name'
'prompt'
>>> re.match('(?P<p>.*?)(?::\s*)(?P<msg>.*)', 'prompt: enter your name').group('msg')
'enter your name'
>>> re.match('(?P<p>.*?)(?::\s*)(?P<msg>.*)', 'prompt: enter your name').group(0)
'prompt: enter your name'
>>> re.match('(?P<p>.*?)(?::\s*)(?P<msg>.*)', 'prompt: enter your name').group(1)
'prompt'
>>> re.match('(?P<p>.*?)(?::\s*)(?P<msg>.*)', 'prompt: enter your name').group(2)
'enter your name'
用 r'\1' 指代匹配
>>> re.sub ( 'id:\s*(?P<id>\d+)', 'N:\\1', 'userlist\nid:001,user001:jiangxin\nid:002,user003:tom\nid:003,user003:jerry\n')
'userlist\nN:001,user001:jiangxin\nN:002,user003:tom\nN:003,user003:jerry\n'
>>> re.sub ( 'id:\s*(?P<id>\d+)', r'N:\1', 'userlist\nid:001,user001:jiangxin\nid:002,user003:tom\nid:003,user003:jerry\n')
'userlist\nN:001,user001:jiangxin\nN:002,user003:tom\nN:003,user003:jerry\n'
>>> re.sub ( 'id:\s*(?P<id>\d+)', 'N:\\1', 'userlist\nid:001,user001:jiangxin\nid:002,user003:tom\nid:003,user003:jerry\n')
'userlist\nN:001,user001:jiangxin\nN:002,user003:tom\nN:003,user003:jerry\n'
>>> re.sub ( 'id:\s*(?P<id>\d+)', r'N:\1', 'userlist\nid:001,user001:jiangxin\nid:002,user003:tom\nid:003,user003:jerry\n')
'userlist\nN:001,user001:jiangxin\nN:002,user003:tom\nN:003,user003:jerry\n'
正则表达式特殊字符
\d When the UNICODE flag is not specified, matches any decimal digit; this is equivalent to the set [0-9]. With UNICODE, it will match whatever is classified as a digit in the Unicode character properties database.
\D When the UNICODE flag is not specified, matches any non-digit character; this is equivalent to the set [^0-9]. With UNICODE, it will match anything other than character marked as digits in the Unicode character properties database.
\s When the LOCALE and UNICODE flags are not specified, matches any whitespace character; this is equivalent to the set [ \t\n\r\f\v]. With LOCALE, it will match this set plus whatever characters are defined as space for the current locale. If UNICODE is set, this will match the characters [ \t\n\r\f\v] plus whatever is classified as space in the Unicode character properties database.
\S When the LOCALE and UNICODE flags are not specified, matches any non-whitespace character; this is equivalent to the set [^ \t\n\r\f\v] With LOCALE, it will match any character not in this set, and not defined as space in the current locale. If UNICODE is set, this will match anything other than [ \t\n\r\f\v] and characters marked as space in the Unicode character properties database.
\w When the LOCALE and UNICODE flags are not specified, matches any alphanumeric character and the underscore; this is equivalent to the set [a-zA-Z0-9_]. With LOCALE, it will match the set [0-9_] plus whatever characters are defined as alphanumeric for the current locale. If UNICODE is set, this will match the characters [0-9_] plus whatever is classified as alphanumeric in the Unicode character properties database.
\W When the LOCALE and UNICODE flags are not specified, matches any non-alphanumeric character; this is equivalent to the set [^a-zA-Z0-9_]. With LOCALE, it will match any character not in the set [0-9_], and not defined as alphanumeric for the current locale. If UNICODE is set, this will match anything other than [0-9_] and characters marked as alphanumeric in the Unicode character properties database.
re.sub(pattern, repl, string[, count])
>>> re.sub ( 'id:\s*(?P<id>\d+)', 'N:\\1', 'userlist\nid:001,user001:jiangxin\nid:002,user003:tom\nid:003,user003:jerry\n')
'userlist\nN:001,user001:jiangxin\nN:002,user003:tom\nN:003,user003:jerry\n'
>>> re.sub ( 'id:\s*(?P<id>\d+)', r'N:\1', 'userlist\nid:001,user001:jiangxin\nid:002,user003:tom\nid:003,user003:jerry\n')
'userlist\nN:001,user001:jiangxin\nN:002,user003:tom\nN:003,user003:jerry\n'
'userlist\nN:001,user001:jiangxin\nN:002,user003:tom\nN:003,user003:jerry\n'
>>> re.sub ( 'id:\s*(?P<id>\d+)', r'N:\1', 'userlist\nid:001,user001:jiangxin\nid:002,user003:tom\nid:003,user003:jerry\n')
'userlist\nN:001,user001:jiangxin\nN:002,user003:tom\nN:003,user003:jerry\n'
compile 对象
getopt(命令行处理)
示例:
>>> import getopt
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
>>> args
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'abc:d:')
>>> optlist
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> args
['a1', 'a2']
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
>>> args
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'abc:d:')
>>> optlist
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> args
['a1', 'a2']
"""Module docstring.
This serves as a long usage message.
"""
import sys
import getopt
def main():
# parse command line options
try:
opts, args = getopt.getopt(sys.argv[1:], "hp:", ["help", "port="])
except getopt.error, msg:
print msg
print "for help use --help"
sys.exit(2)
# process options
for o, a in opts:
if o in ("-h", "--help"):
print __doc__
sys.exit(0)
elif o in ("-p", "--port"):
print "port is %d" % a
# process arguments
for arg in args:
process(arg) # process() is defined elsewhere
if __name__ == "__main__":
main()
This serves as a long usage message.
"""
import sys
import getopt
def main():
# parse command line options
try:
opts, args = getopt.getopt(sys.argv[1:], "hp:", ["help", "port="])
except getopt.error, msg:
print msg
print "for help use --help"
sys.exit(2)
# process options
for o, a in opts:
if o in ("-h", "--help"):
print __doc__
sys.exit(0)
elif o in ("-p", "--port"):
print "port is %d" % a
# process arguments
for arg in args:
process(arg) # process() is defined elsewhere
if __name__ == "__main__":
main()
logging
getLogger()
logging.basicConfig()
logging.getLogger("").setLevel(logging.DEBUG)
ERR = logging.getLogger("ERR")
ERR = logging.getLogger("ERR")
ERR.setLevel(logging.ERROR)
#These should log
logging.log(logging.CRITICAL, nextmessage())
logging.debug(nextmessage())
ERR.log(logging.CRITICAL, nextmessage())
ERR.error(nextmessage())
#These should not log
ERR.debug(nextmessage())
logging.getLogger("").setLevel(logging.DEBUG)
ERR = logging.getLogger("ERR")
ERR = logging.getLogger("ERR")
ERR.setLevel(logging.ERROR)
#These should log
logging.log(logging.CRITICAL, nextmessage())
logging.debug(nextmessage())
ERR.log(logging.CRITICAL, nextmessage())
ERR.error(nextmessage())
#These should not log
ERR.debug(nextmessage())
Python 实战
命令行处理
命令行框架
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import getopt
def main(argv=None):
if argv is None:
argv = sys.argv
try:
opts, args = getopt.getopt(
argv[1:], "hn:",
["help", "name="])
except getopt.error, msg:
return usage(1, msg)
for opt, arg in opts:
if opt in ('-h', '--help'):
return usage(0)
#elif opt in ('--more_options'):
if __name__ == "__main__":
sys.exit(main())
# -*- coding: utf-8 -*-
import sys
import getopt
def main(argv=None):
if argv is None:
argv = sys.argv
try:
opts, args = getopt.getopt(
argv[1:], "hn:",
["help", "name="])
except getopt.error, msg:
return usage(1, msg)
for opt, arg in opts:
if opt in ('-h', '--help'):
return usage(0)
#elif opt in ('--more_options'):
if __name__ == "__main__":
sys.exit(main())
unicode
认识 unicode
# 因为当前 locale 是 utf-8 编码,因此字符串默认编码为 utf-8
>>> '中文'
'\xe4\xb8\xad\xe6\x96\x87'
>>> isinstance('中文', unicode)
False
>>> isinstance('中文', str)
True
>>> '中文'
'\xe4\xb8\xad\xe6\x96\x87'
>>> isinstance('中文', unicode)
False
>>> isinstance('中文', str)
True
# decode 是将 str 转换为 unicode
>>> '中文'.decode('utf-8')
u'\u4e2d\u6587'
>>> isinstance('中文'.decode('utf-8'), unicode)
True
>>> isinstance('中文'.decode('utf-8'), str)
False
>>> '中文'.decode('utf-8')
u'\u4e2d\u6587'
>>> isinstance('中文'.decode('utf-8'), unicode)
True
>>> isinstance('中文'.decode('utf-8'), str)
False
# 前缀 u 定义 unicode 字串
>>> u'中文'
u'\u4e2d\u6587'
>>> isinstance(u'中文', unicode)
True
>>> isinstance(u'中文', str)
False
>>> u'中文'
u'\u4e2d\u6587'
>>> isinstance(u'中文', unicode)
True
>>> isinstance(u'中文', str)
False
Unicode 典型错误1
Unicode 典型错误2
mystr = '中文'
mystr.encode('gb18030')
报错:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
mystr.encode('gb18030')
报错:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
错误解析:
mystr.encode('gb18030') 这句代码将 mystr 重新编码为 gb18030 的格式,即进行 unicode -> str 的转换。因为 mystr 本身就是 str 类型的,因此 Python 会自动的先将 mystr 解码为 unicode ,然后再编码成 gb18030。
因为解码是python自动进行的,我们没有指明解码方式,python 就会使用 sys.defaultencoding 指明的方式来解码。很多情况下 sys.defaultencoding 是 ANSCII,如果 mystr 不是这个类型就会出错。
拿上面的情况来说,缺省 sys.defaultencoding 是 anscii,而 mystr 的编码方式和文件的编码方式一致,是 utf8 的,所以出错了。
mystr.encode('gb18030') 这句代码将 mystr 重新编码为 gb18030 的格式,即进行 unicode -> str 的转换。因为 mystr 本身就是 str 类型的,因此 Python 会自动的先将 mystr 解码为 unicode ,然后再编码成 gb18030。
因为解码是python自动进行的,我们没有指明解码方式,python 就会使用 sys.defaultencoding 指明的方式来解码。很多情况下 sys.defaultencoding 是 ANSCII,如果 mystr 不是这个类型就会出错。
拿上面的情况来说,缺省 sys.defaultencoding 是 anscii,而 mystr 的编码方式和文件的编码方式一致,是 utf8 的,所以出错了。