类似C中的?表达式,但只有当a不为''时才有效
进阶 bool and [a] or [b]
即使a为'',[a]仍为非空
or 返回第一个真值,或者最后一个假值
and or 表达式从左到右运算,优先级相同
若运算对象为大型list,extend性能更好
List []
Tuple ()
Python 支持多重继承,在类定义中用逗号分隔列出多个父类即可
在子类中调用父类方法时,要显式列出self
类外调用方法时不需要写出self
...
except Error
...
else
...
getattr() 通关module名获取module的引用
sys.module()
os.path.split()
os.path.splitext()
os.listdir()
os.path.isfile()
os.path.isdir()
os.getcwd()
os.path.normcase()
module glob
glob.glob() 可匹配所有子目录
可实现动态的将类看成对象并传入参数,动态的实例化未知类型的类。
可讲编译器视作小狗,需要用合适的语言进行交流。
iter()
globals() 当前Python程序空间范围,返回实际的全局命名空间,更改变量会造成变量实际改变
from module import 是将指定方法导入locals()空间,import module 则是将对象module导入locals()空间
def print_params_4(x, y, z=3, *pospar, **keypar):
print x, y, z
print pospar
print keypar
A function such as multiplyByFactor that stores it enclosing scopes is called a closure.
If a function or an algorithm is complex and difficult to understand, clearly defining it in your own language before implementing it can be very useful. aka. pseudocode.
递归可以稍微增加函数的可读性
lambda表达式:mainly used to build in-line function
声明类的私有方法时,需要在方法前加上双下划线 __inaccessible
MRO method resolution order 查询父类中属性或者方法的顺序,Python内置算法
python支持多重继承
不同父类中有同名的方法时,优先使用声明时写在前面的父类的方法,并会使后面父类中的同名方法不可访问。(多重继承父类顺序很重要!)
dict.get(name[, default]) either create or update
try:/catch exception: 捕捉异常
raise Exception 抛出异常,若raise后无参数异常,则抛出最近的一个异常(也可能为raise Empty,总会抛出一个异常)。
Any function that contains a yield statement is called a generator.
def flatten(nested):
for sublist in nested:
for element in sublist:
yield element
package import的时候只是执行package目录下的__init__.py,如果__init__.py中没有import package内的module,则package内部的module仍不可用,如果需要使用,则仍需显式import
两种显式import package中module的方式:
import package.module
from package import module
若__init__.py未import module,只import package有什么用?
re
the period (dot) can match anything, so is called a wildcard (通配符).
^ to invert the character set. '[^abc]' matches any character except a, b, or c
? nongreedy mode
emphasis_pattern = r'**(.*?)**' (.*?) matches the least substring needed to the next '**'
set can remove duplicated elements
module email processes text saved email files
pat = re.compile(r'[a-z-.]+@[a-z-.]+', re.IGNORECASE)
pat.match().group(1) MatchObject.group(0) is the entire string.
zip() 使用指定的list作为字典的键和值,建立字典
>>> a = [1, 2, 3]
>>> b = ['j', 'v', 'm']
>>> dic = dict(zip(a, b))
>>> dic
{1: 'j', 2: 'v', 3: 'm'}
timeit a more efficient module for code performance measurements
profile similar to timeit, but with a more comprehensive result
trace a module that can provide coverage analysis
itertools used for operating iterable objects
logging provides a set of tools managing one or more central log
getopt optparse used for processing parameters of Python script
optparse is newer, more efficient and easier
cmd enables you to write a command line interpreter in which you can define your own commands. A personal specific prompt
Packages are implemented as directories that contain a file named __init__.py.
Exploring modules use dir, examine the __all__ variable, and use help
fileinput: A module that makes it easy to interate over the lines of several files or streams.
shelve: A module for creating a persistent mapping, which stores its contents in a database with a given name.
with help the user to manage files, returns a fileobject and automatically call the close method at the end of the with block
with open('d:\somefile.txt') as f:
print type(f)
If you want to make sure your file is closed, even if something goes wrong, you can use the with statement.
file contents can be iterated directly:
for line in open(filename):
process(line)
python中整型与float比较时,float会被round up,需要将整型转为float再做比较
注意直接用list转化string,会转化为单个字符的list
>>> list('guttagonol')
['g', 'u', 't', 't', 'a', 'g', 'o', 'n', 'o', 'l']
直接加[]才会是整个string的list
>>> ['guttagonol']
['guttagonol']
对已经import的模块进行修改后,需要重新导入时,要使用reload()函数。
swap
a, b = b, a
类似C中的?表达式,但只有当a不为''时才有效
进阶 bool and [a] or [b]
即使a为'',[a]仍为非空
or 返回第一个真值,或者最后一个假值
and or 表达式从左到右运算,优先级相同
若运算对象为大型list,extend性能更好
List []
Tuple ()
Python 支持多重继承,在类定义中用逗号分隔列出多个父类即可
在子类中调用父类方法时,要显式列出self
类外调用方法时不需要写出self
...
except Error
...
else
...
getattr() 通关module名获取module的引用
sys.module()
os.path.split()
os.path.splitext()
os.listdir()
os.path.isfile()
os.path.isdir()
os.getcwd()
os.path.normcase()
module glob
glob.glob() 可匹配所有子目录
可实现动态的将类看成对象并传入参数,动态的实例化未知类型的类。
可讲编译器视作小狗,需要用合适的语言进行交流。
iter()
globals() 当前Python程序空间范围,返回实际的全局命名空间,更改变量会造成变量实际改变
from module import 是将指定方法导入locals()空间,import module 则是将对象module导入locals()空间
def print_params_4(x, y, z=3, *pospar, **keypar):
print x, y, z
print pospar
print keypar
A function such as multiplyByFactor that stores it enclosing scopes is called a closure.
If a function or an algorithm is complex and difficult to understand, clearly defining it in your own language before implementing it can be very useful. aka. pseudocode.
递归可以稍微增加函数的可读性
lambda表达式:mainly used to build in-line function
声明类的私有方法时,需要在方法前加上双下划线 __inaccessible
MRO method resolution order 查询父类中属性或者方法的顺序,Python内置算法
python支持多重继承
不同父类中有同名的方法时,优先使用声明时写在前面的父类的方法,并会使后面父类中的同名方法不可访问。(多重继承父类顺序很重要!)
dict.get(name[, default]) either create or update
try:/catch exception: 捕捉异常
raise Exception 抛出异常,若raise后无参数异常,则抛出最近的一个异常(也可能为raise Empty,总会抛出一个异常)。
Any function that contains a yield statement is called a generator.
def flatten(nested):
for sublist in nested:
for element in sublist:
yield element
package import的时候只是执行package目录下的__init__.py,如果__init__.py中没有import package内的module,则package内部的module仍不可用,如果需要使用,则仍需显式import
两种显式import package中module的方式:
import package.module
from package import module
若__init__.py未import module,只import package有什么用?
re
the period (dot) can match anything, so is called a wildcard (通配符).
^ to invert the character set. '[^abc]' matches any character except a, b, or c
? nongreedy mode
emphasis_pattern = r'**(.*?)**' (.*?) matches the least substring needed to the next '**'
set can remove duplicated elements
module email processes text saved email files
pat = re.compile(r'[a-z-.]+@[a-z-.]+', re.IGNORECASE)
pat.match().group(1) MatchObject.group(0) is the entire string.
zip() 使用指定的list作为字典的键和值,建立字典
>>> a = [1, 2, 3]
>>> b = ['j', 'v', 'm']
>>> dic = dict(zip(a, b))
>>> dic
{1: 'j', 2: 'v', 3: 'm'}
timeit a more efficient module for code performance measurements
profile similar to timeit, but with a more comprehensive result
trace a module that can provide coverage analysis
itertools used for operating iterable objects
logging provides a set of tools managing one or more central log
getopt optparse used for processing parameters of Python script
optparse is newer, more efficient and easier
cmd enables you to write a command line interpreter in which you can define your own commands. A personal specific prompt
Packages are implemented as directories that contain a file named __init__.py.
Exploring modules use dir, examine the __all__ variable, and use help
fileinput: A module that makes it easy to interate over the lines of several files or streams.
shelve: A module for creating a persistent mapping, which stores its contents in a database with a given name.
with help the user to manage files, returns a fileobject and automatically call the close method at the end of the with block
with open('d:\somefile.txt') as f:
print type(f)
If you want to make sure your file is closed, even if something goes wrong, you can use the with statement.
file contents can be iterated directly:
for line in open(filename):
process(line)
python中整型与float比较时,float会被round up,需要将整型转为float再做比较
注意直接用list转化string,会转化为单个字符的list
>>> list('guttagonol')
['g', 'u', 't', 't', 'a', 'g', 'o', 'n', 'o', 'l']
直接加[]才会是整个string的list
>>> ['guttagonol']
['guttagonol']
对已经import的模块进行修改后,需要重新导入时,要使用reload()函数。
swap
a, b = b, a
类似C中的?表达式,但只有当a不为''时才有效
进阶 bool and [a] or [b]
即使a为'',[a]仍为非空
or 返回第一个真值,或者最后一个假值
and or 表达式从左到右运算,优先级相同
若运算对象为大型list,extend性能更好
List []
Tuple ()
Python 支持多重继承,在类定义中用逗号分隔列出多个父类即可
在子类中调用父类方法时,要显式列出self
类外调用方法时不需要写出self
...
except Error
...
else
...
getattr() 通关module名获取module的引用
sys.module()
os.path.split()
os.path.splitext()
os.listdir()
os.path.isfile()
os.path.isdir()
os.getcwd()
os.path.normcase()
module glob
glob.glob() 可匹配所有子目录
可实现动态的将类看成对象并传入参数,动态的实例化未知类型的类。
可讲编译器视作小狗,需要用合适的语言进行交流。
iter()
globals() 当前Python程序空间范围,返回实际的全局命名空间,更改变量会造成变量实际改变
from module import 是将指定方法导入locals()空间,import module 则是将对象module导入locals()空间
def print_params_4(x, y, z=3, *pospar, **keypar):
print x, y, z
print pospar
print keypar
A function such as multiplyByFactor that stores it enclosing scopes is called a closure.
If a function or an algorithm is complex and difficult to understand, clearly defining it in your own language before implementing it can be very useful. aka. pseudocode.
递归可以稍微增加函数的可读性
lambda表达式:mainly used to build in-line function
声明类的私有方法时,需要在方法前加上双下划线 __inaccessible
MRO method resolution order 查询父类中属性或者方法的顺序,Python内置算法
python支持多重继承
不同父类中有同名的方法时,优先使用声明时写在前面的父类的方法,并会使后面父类中的同名方法不可访问。(多重继承父类顺序很重要!)
dict.get(name[, default]) either create or update
try:/catch exception: 捕捉异常
raise Exception 抛出异常,若raise后无参数异常,则抛出最近的一个异常(也可能为raise Empty,总会抛出一个异常)。
Any function that contains a yield statement is called a generator.
def flatten(nested):
for sublist in nested:
for element in sublist:
yield element
package import的时候只是执行package目录下的__init__.py,如果__init__.py中没有import package内的module,则package内部的module仍不可用,如果需要使用,则仍需显式import
两种显式import package中module的方式:
import package.module
from package import module
若__init__.py未import module,只import package有什么用?
re
the period (dot) can match anything, so is called a wildcard (通配符).
^ to invert the character set. '[^abc]' matches any character except a, b, or c
? nongreedy mode
emphasis_pattern = r'**(.*?)**' (.*?) matches the least substring needed to the next '**'
set can remove duplicated elements
module email processes text saved email files
pat = re.compile(r'[a-z-.]+@[a-z-.]+', re.IGNORECASE)
pat.match().group(1) MatchObject.group(0) is the entire string.
zip() 使用指定的list作为字典的键和值,建立字典
>>> a = [1, 2, 3]
>>> b = ['j', 'v', 'm']
>>> dic = dict(zip(a, b))
>>> dic
{1: 'j', 2: 'v', 3: 'm'}
timeit a more efficient module for code performance measurements
profile similar to timeit, but with a more comprehensive result
trace a module that can provide coverage analysis
itertools used for operating iterable objects
logging provides a set of tools managing one or more central log
getopt optparse used for processing parameters of Python script
optparse is newer, more efficient and easier
cmd enables you to write a command line interpreter in which you can define your own commands. A personal specific prompt
Packages are implemented as directories that contain a file named __init__.py.
Exploring modules use dir, examine the __all__ variable, and use help
fileinput: A module that makes it easy to interate over the lines of several files or streams.
shelve: A module for creating a persistent mapping, which stores its contents in a database with a given name.
with help the user to manage files, returns a fileobject and automatically call the close method at the end of the with block
with open('d:\somefile.txt') as f:
print type(f)
If you want to make sure your file is closed, even if something goes wrong, you can use the with statement.
file contents can be iterated directly:
for line in open(filename):
process(line)
python中整型与float比较时,float会被round up,需要将整型转为float再做比较
注意直接用list转化string,会转化为单个字符的list
>>> list('guttagonol')
['g', 'u', 't', 't', 'a', 'g', 'o', 'n', 'o', 'l']
直接加[]才会是整个string的list
>>> ['guttagonol']
['guttagonol']
对已经import的模块进行修改后,需要重新导入时,要使用reload()函数。
swap
a, b = b, a
When two classes share some common behavior, it's probably better to create a generic superclass that both classes can inherit.
字典的KeyError异常:当请求字典对象里面没有的key时,python会抛出异常KeyError。