标签:
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
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
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。
标签:
原文地址:http://www.cnblogs.com/harelion/p/4856175.html