Python 2.4 Quick Reference Card
©2005-2007 — Laurent Pointal — License CC [by nc sa] C ARD C ONTENT
Environment Variables (1)
Command-line Options (1)
Files Extensions (1)
Language Keywords (1)
Builtins (1)
Types (1)
Functions (1)
Statements (1)
Blocks (1)
Assignment Shortcuts (1)
Console & Interactive Input/Output (1)
Objects, Names and Namespaces (2)
Identifiers (2)
Objects and Names, Reference Counting..2 Mutable/Immutable Objects (2)
Namespaces (2)
Constants, Enumerations (2)
Flow Control (2)
Condition (2)
Loop (2)
Functions/methods exit (2)
Exceptions (2)
Iterable Protocol (2)
Interpretation / Execution (2)
Functions Definitions & Usage (2)
Parameters / Return value (2)
Lambda functions (2)
Callable Objects (2)
Calling Functions (2)
Functions Control (2)
Decorators (2)
Types/Classes & Objects (3)
Class Definition (3)
Object Creation (3)
Classes & Objects Relations (3)
Attributes Manipulation (3)
Special Methods (3)
Descriptors protocol (3)
Copying Objects (3)
Introspection (3)
Modules and Packages (3)
Source encodings (3)
Special Attributes (3)
Main Execution / Script Parameters (3)
Operators (4)
Priority (4)
Arithmetic Operators (4)
Comparison Operators (4)
Operators as Functions (4)
Booleans (4)
Numbers (4)
Operators (4)
Functions (4)
Bit Level Operations (5)
Operators (5)
Strings (5)
Escape sequences (5)
Unicode strings (5)
Methods and Functions (5)
Formating (5)
Constants (6)
Regular Expressions (6)
Localization (6)
Multilingual Support (7)
Containers (7)
Operations on Containers (7)
Copying Containers (8)
Overriding Containers Operations (8)
Sequences (8)
Lists & Tuples (8)
Operations on Sequences (8)
Indexing (8)
Operations on mutable sequences (8)
Overriding Sequences Operations (8)
Mappings (dictionaries) (8)
Operations on Mappings (8)
Overriding Mapping Operations (8)
Other Mappings (8)
Sets (8)
Operations on Sets (8)
Other Containers Structures, Algorithms9
Array (9)
Queue (9)
Priority Queues (9)
Sorted List (9)
Iteration Tools (9)
Date & Time (9)
Module time (9)
Module datetime (9)
Module timeit (9)
Other Modules (10)
Files (10)
File Objects (10)
Low-level Files (10)
Pipes (10)
In-memory Files (10)
Files Informations (10)
Terminal Operations (11)
Temporary Files (11)
Path Manipulations (11)
Directories (11)
Special Files (11)
Copying, Moving, Removing (11)
Encoded Files (11)
Serialization (12)
Persistence (12)
Configuration Files (12)
Exceptions (12)
Standard Exception Classes (12)
Warnings (12)
Exceptions Processing (12)
Encoding - Decoding (12)
Threads & Synchronization (12)
Threading Functions (12)
Threads (13)
Mutual Exclusion (13)
Events (13)
Semaphores (13)
Condition Variables (13)
Synchronized Queues (13)
Process (13)
Current Process (13)
Signal Handling (14)
Simple External Process Control (14)
Advanced External Process Control (14)
XML Processing (15)
SAX - Event-driven (15)
DOM - In-memory Tree (16)
Databases (17)
Generic access to DBM-style DBs (17)
Standard DB API for SQL databases (17)
Bulk (17)
Styles : keyword function/method type replaced_expression variable
literal module module_filename language_syntax
Notations :
f(…)→ return value f(…)➤ return nothing (procedure)
[x] for a list of x data, (x) for a tuple of x data, may have x{n}→
n times x data.
E NVIRONMENT V ARIABLES
PYTHONCASEOK1 no case distinction in module→file mapping
PYTHONDEBUG1 = -d command-line option
PYTHONHOME Modify standard Python libs prefix and exec prefix
locations. Use <prefix>[:<execprefix>].
PYTHONINSPECT1 = -i command-line option
PYTHONOPTIMIZE1 = -O command-line option
PYTHONPATH Directories where Python search when importing
modules/packages. Separator : (posix) or ;
(windows). Under windows use registry
HKLM\Sofware\….
PYTHONSTARTUP File to load at begining of interactive sessions.
PYTHONUNBUFFERED1 = -u command-line option
PYTHONVERBOSE1 = -v command-line option
1 If set to non-empty value.
C OMMAND-LINE O PTIONS
python[-dEhiOQStuUvVWx][-c cmd| -m mod|file| -][args]
-d Output debugging infos from parser.
-E Ignore environment variables.
-h Print help and exit.
-i Force interactive mode with prompt (even after script
execution).
-O Optimize generated bytecode, remove assert checks.
-OO As -O and remove documentation strings.
-Q arg Division option, arg in [old(default),warn,warnall,new].
-S Don't import site.py definitions module.
-
t Warn inconsistent tab/space usage (-tt exit with error).
-u Use unbuffered binary output for stdout and stderr.
-U Force use of unicode literals for strings.
-v Trace imports.
-V Print version number and exit.
-W arg Emit warning for arg "action:message:category:module:lineno"
-x Skip first line of source (fort non-Unix forms of #!cmd).
-c cmd Execute cmd.
-m mod Search module mod in sys.path and runs it as main script.
file Python script file to execute.
args Command-line arguments for cmd/file, available in
sys.argv[1:].
F ILES E XTENSIONS
.py=source, .pyc=bytecode, .pyo=bytecode optimized, .pyd=binary
module, .dll/.so=dynamic library.
.pyw=source associated on Windows platform, to run
without opening a console.
exec finally for from global if import in is lambda not or
Available directly everywhere with no specific import. Defined also in
module __builtins__.
Types
basestring1 bool buffer complex dict exception file float
frozenset int list long object set slice str tuple type
unicode xrange
1 basestring is virtual superclass of str and unicode.
This doc uses string when unicode and str can apply.
Functions
Constructor functions of builtin types are directly accessible in builtins.
__import__ abs apply1 callable chr classmethod cmp coerce
compile delattr dir divmod enumerate eval execfile filter
getattr globals hasattr hash help hex id input intern2
isinstance issubclass iter len locals map max min oct open
ord pow property range raw_input reduce reload repr reversed
round setattr sorted staticmethod sum super unichr vars zip
1 Use f(*args,**kargs) in place of apply(f,args,kargs).
2 Don't use intern.
S TATEMENTS
One statement per line1. Can continue on next line if an expression or a
string is not finished ( ([{"""''' not closed), or with a \ at end of
line.
Char # start comments up to end of line.
pass Null statement.
assert expr[,message]Assertion check expression true.
del name[,…]Remove name → object binding.
print[>>obj,][expr[,…][,]Write expr to sys.stdout2.
exec expr[in globals[, locals]]Execute expr in namespaces.
fct([expr[,…]],[name= expr[,…]]
[,*args][,**kwargs])
Call any callable object fct with given
arguments (see Functions Definitions &
Usage - p2).
name[,…]=expr Assignment operator3.
1 Multiple statements on same line using ; separator - avoid if not
necessary.
2 Write to any specified object following file interface (write method).
Write space between expressions, line-return at end of line except with
a final ,.
3 Left part name can be container expression. If expr is a sequence of
multiple values, can unpack into multiple names. Can have multiple
assignments of same value on same line : a = b = c = expr.
Other statements (loops, conditions…) introduced in respective parts.
Blocks
A : between statements defines dependant statements, written on same
line or written on following line(s) with deeper indentation.
Blocks of statements are simply lines at same indentation level.
if x<=0 : return 1
if asin(v)>pi/4 :
a = pi/2
b = -pi/2
else :
a = asin(v)
b = pi/2-a
Statement continuation lines don't care indentation.
To avoid problems, configure your editor to use 4 spaces in place of
tabs.
Assignment Shortcuts
a+=b a-=b a*=b a/=b
a//=b a%=b a**=b
a&=b a|=b a^=b a>>=b a<<=b
Evaluate a once, and assign to a the result of operator before = applied
to current a and b. Example : a%=b ≈ a=a%b
C ONSOLE & I NTERACTIVE I NPUT/O UTPUT
print expression[,…]
input([prompt]) → evaluation of user input (typed data)
raw_input([prompt]) → str: user input as a raw string
Direct manipulation (redefinition) of stdin/stdout/stderr via sys module : sys.stdin sys.stdout sys.stderr
sys.__stdin__sys.__stdout__sys.__stderr__
All are files or files-like objects. The __xxx__ forms keep access to original standard IO streams.
Ctrl-C raises KeyboardInterrupt exception.
_→ value of last expression evaluation
help([object])➤ print online documentation
sys.displayhook→ (rw) fct(value) called to display value
sys.__displayhook__→ backup of original displayhook function
sys.ps1 → str: primary interpreter prompt
sys.ps2 → str: secondary (continuation) interpreter prompt
See external package ipython for an enhanced interactive Python shell. O BJECTS, N AMES AND N AMESPACES
Identifiers
Use : [a-zA-Z_][a-zA-Z0-9_]*
Special usage for underscore :
_xxx global not imported by import *
_xxx implementation detail, for internal use (good practice)
__xxx'private' class members, defined as _ClassName__xxx
__xxx__normally reserved by Python
Case is significant : This_Name != THIS_NAME.
Objects and Names, Reference Counting
Data are typed objects (all data), names are dynamically bound to objects.
= assignment statement bind result of right part evaluation into left part name(s)/container(s). Examples :
a = 3*c+5
s = "Hello"a,b = ("Hello","World")
pi,e = 3.14,2.71
x,y,tabz[i] = fct(i)
a,b = b,a
When an object is no longer referenced (by names or by containers), it is destroyed (its __del__ method is then called).
Mutable/Immutable Objects
Mutable objects can be modified in place. Immutable objects cannot be modified (must build a new object with new value).
Immutable : bool, int, long, float, complex, string, unicode, tuple, frozenset, buffer, slice.
Mutable : list, set, dict and other high level class objects.
There is no constant definition. Just use uppercase names to identify symbols which must not be modified.
Namespaces
Places where Python found names.
Builtins namespace → names from module __builtins__, already available.
Global namespace → names defined at module level (zero indentation). Local namespace → names defined in methods/functions.
del name ➤ remove existing name from namespace (remove object binding) globals()→ dict: identifier→value of global namespace
locals() → dict: identifier→value of local namespace
Current scope → names directly usable. Searched in locals, then locals from enclosing definitions, then globals, then builtins.
Out-of-scope name → use the dotted attribute notation x.y (maybe
<)… where x is a name visible within the current scope.
Class namespace → names defined in a class (class members).
Object namespace → names usable with object.name notation (attributes,
methods).
Namespaces can be nested, inner namespaces hidding identical names
from outer namespaces.
dir([object]) → list: names defined in object namespace1
vars([object]) → dict2: identifier:value of object as a namespace1
1 if object not specified use nearest namespace (locals).
2 must not be modified.
Constants, Enumerations
Use uppercase and _ for constants identifiers (good practice). May define
namespaces to group constants. Cannot avoid global/local name
redefinition (can eventually define namespaces as classes with
attributes access control - not in Python spirit, and execution cost).
See third party modules pyenum for strict enum-like namespace.
F LOW C ONTROL
Condition
if cond:inst
[elif cond:inst]
[else:inst]
There is no 'switch' or 'case'.
Can use if elif elif… else.
Can use a mapping with
Loop
for
var[,…]in iterable:inst
[else:inst]
while cond:inst
[else:inst]
Exit loop with break.
Go to next iteration with continue.
else blocs only executed when
break).
[value]
Exit from generator body with yield value
Multiple returned values using tuple data.
Cannot yield within a try/finally block.
Exceptions
try:inst
except[except_class[,v alue]]:inst
…
[else:inst]
Can have a tuple of classes for except_class. Not specifying a class
catch all exceptions.
Block else executed when try block exit normally.
try:inst
finally:inst
Process finally block in all execution paths (normal or exception).
raise exception_class[,value[,traceback]]
raise exception_object
raise
Last form re-raise the currently catched exception in an exception
handler.
Iterable Protocol
Generic and simple protocol allowing to iterate on any collection of data.
Objects of class defining __iter__ or __getitem__ are iterable (directly
usable in for loops).
__iter__(self) → iterator on self
iter(object) → iterator on iterable object
iter(callable,sentinel) → iterator returning callable() values up to sentinel
enumerate(iterable)→ iterator returning tuples (index,value) from iterable
Iterators Objects Interface
next(self)→ next item1
__iter__(self)→ iterator object itself
1 When reach end of collection, raise StopIteration exception on
subsequent calls (ie. iterator usable only one time on a collection).
Generators
Functions retaining their state between two calls. Return values using
yield. Stop generation via simple return or via raise StopIteration.
1) build generator from function : gen=generatorfct(args)
2) () values until StopIteration is raised.
Generator iterable expressions with : (x for x in iterable where cond)
Operations with/on Iterable
See Operations on Containers (p7).
See Iteration Tools (p9).
I NTERPRETATION / E XECUTION
compile(string1,filename,kind2[,flags3[,dont_inherit3]]) → code object
eval(expression[,globals[,locals]]) → value: evaluation4 of expression string
eval(code_object[,globals[,locals]]) → value: evaluation4 of code_object
exec5statements [in globals[,locals]] ➤ statements string1 executed4
execfile(filename[,globals[,locals]]) ➤ file filename interpreted4
def fctname([paramname[= defaultvalue][,…]] [,*args][,**kwargs]):
instructions
new.function(code,globals[,name[,argdefs]]) → python function (see docs)
Parameters / Return value
大学python知识点汇总Parameters are passed by references to objects.
You can modify values of mutable objects types.
You cannot modify values of immutable objects types - as if they were
passed by value.
Notation * → variable list of anonymous parameters in a tuple.
Notation ** → variable list of named parameters in a dict.
Return value(s) with return[value[,…]]
For multiple values, return a tuple. If no return value specified or if end
of function definition reached, return None value.
Lambda functions
lambda param[,…]:expression
Anonymous functions defined inline. Result of expression evaluation is
returned (it must be an expression, no loop, no condition).
Expression uses values known at definition time (except for params).
Callable Objects
Objects having a __call__ method can be used as functions.
Methods bound to objects can be used as functions : h
callable(x) → bool: test x callable with x(…)
Calling Functions
[name=]fctname([expr[,…]][,name=expr[,…][,*args][,**args])
Anonymous parameters passed in parameters order declaration.
Params having default value can be omitted.
Notation * → pass variable list of anonymous parameters in a tuple.
Notation ** → pass variable list of named parameters in a dict.
Functions Control
sys.setrecursionlimit(limit) ➤ set recursion limit for functions
Decorators
Glue code (functions) called at functions and methods definitions time,
return the final function/method (generally with wrapping code).
@d ecoratorname[(d ecorator_arguments)] […] def fct(fct_rguments):…
@dec1 @dec2(args) @dec3
def fct(…):…like
➤def fct(…):…
fct=dec1(dec2(args)(dec3(fct))))
See page PythonDecoratorLibrary Wiki for some decorators definitions.
T YPES/C LASSES & O BJECTS
All data are typed objects relying to classes.
type(o) → type: type object of o
Standard module types define type objects for builtins types.
Class Definition
class classname[(parentclass[,…])] :
varname = expr ➤ varname defined in classname namespace
def metname(self[,…]): ➤ define methods like functions
Support multiple inheritance. Can inherit from builtin class.
Inherit at least from object base class =>Python 'new style class'. First parameter of methods is target object, standard use self name. Access class members via class name, object members via self.
This doc consider you use new style class (inheriting from object). new.classobj(name,baseclasses,dict) → new class (see docs)
new.instancemethod(fct,instance,class) → new method: bound to instance it it is not None, see docs
Metaclass
Class definition create a new type. It can be done 'by hand' with :
x = type('classname',(parentclass,[…]),{varname:expr[,…]}
def metname(self[,…]):
This allow creation of metaclass class (class building other class).
Object Creation
obj=ClassName(initargs…)
In case of exception during initialization, object is destroyed when exiting init code (reference counter reach zero).
new.instance(class[,dict]) → object: create new class instance without calling __init__ method, dict is initial object attributes
Classes & Objects Relations
isinstance(obj,classinfo) → bool: test object kind of type/class classinfo issubclass(aclass,aparent) → bool: test same class or parent relationship Prefer isinstance() to type() for type checking.
Parent class methods are not automatically called if overriden in subclass - they must be explicitly called if necessary.
Call parent methods via super function :
super(ThisClass,self).methodname(self,args…)
Or the old way, via parent class namespace :
Attributes Manipulation
object.name=value
setattr(object,name,value) ➤ object attribute set to value
object.name→value of object attribute
getattr(object,name[,default])→ value of object attribute
del object.name
delattr(object,name) ➤ object attribute removed
Special Methods
Other special overridable __xxx___ methods are listed in respective sections.
Object Life
__new__(classref,initargs…)→ object of classref type, already initialized1 __init__ (self,initargs…)➤ called to initialize object with initargs
__del__(self)➤ called when object will be destroyed
1 If don't return a classref object, then object.__init__ is called with
initargs.
Object Cast
__repr__(self)→ str: called for repr(self) and `self`
__str__(self) → str: called for str(self) and print self
__coerce__(self,other)→ value, called for coerce(self,other)
Object Hash Key
__hash__(self)→ int: 32 bits hash code for object, used for hash(obj)and
quick dict mapping keys comparison - default implementation use
hash(id(self))
Attributes access
See also "Descriptors protocol" infra.
__getattr__(self,name)→ value, called for undefined attributes
__getattribute__(self, name)→ value, always called
__setattr__(self, name,value) ➤ called for obj.name=value
__delattr__(self, name) ➤ called for del obj.name
__call__(self, *args, **kwargs)→ value, called for obj(…)
Static method / Class method
Use standard decorators (see Decorators p2).
class ClassName :
@staticmethod
def methodname(…): …
@classmethod
def methodname(classref,…): …
Descriptors protocol
Descriptors are attribute objects controling access to attributes values.
They must define some of following methods :
__get__(self,obj,ownerclass)→ attribute value for obj
__set__(self,obj,value) ➤ modify attribute in obj, set to value
__delete__(self,obj) ➤ remove attribute from obj
In these methods self is the descriptor object, and obj is the target
object which attribute is manipulated.
Properties
A descriptor to directly bind methods/functions to control attribute
access. Use builtin type property with init args.
class MyClass:
attributename=property(getter,setter,deleter,description)
Each init arg default to None (ie. undefined).
Copying Objects
Assignment only duplicate references. To shallow copy an object (build a
new one with same values - referencing same content), or to deep copy
an object (deep-copying referenced content), see object copy methods,
and functions in standard module copy.
copy.deepcopy(object[[,memo],_nil])→ value: deep copy of object1
1 Params memo and nil are used in recursive deepcopy, their default
values are None and empty list.
Copy Protocol
__copy__(self)→ value: shallow copy of self, called py(…)
__deepcopy__(self,memo)→ value: deep copy of self, called by
copy.deepcopy(…)
For copying, objects can define pickling protocol too (see Files -
Serialization - p12), in place of __copy__ and __deepcopy__.
Introspection
Beyond this documentation. Many __xxx___ attributes are defined, some
are writable (see other docs).
See standard module inspect to manipulate these data.
Example of Introspection Attributes
Note: classes are objects too!
__base__→ list: parent classes of a class
__slots__→ tuple: allowed objects attributes names1 of a class
__class__→ class/type: object's class
__dict__ → dict: defined attributes (object namespace) of an instance
__doc__→ string: documentation string of a package, module, class, function
__name__ → str: object definition name of a function
__file__ → string: pathname of loaded module .pyc, .pyo or .pyd
1 List of allowed attributes names. Usage discouraged.
M ODULES AND P ACKAGES
File gabuzo.py ➤ module gabuzo.
Directory kramed/ with a file __init__.py➤ p ackage kramed.
Can have sub-packages (subdirectories having __init__.py file).
Searched in the Python PATH.
Current Python PATH stored in sys.path list. Contains directories and
.zip files paths. Built from location of standard Python modules,
PYTHONPATH environment variable, directory of main module given on
command line, data specified in lines of .pth files found in Python home
directory, and data specified in registry under Windows.
Current list of loaded modules stored dules map (main module
is under key __main__).
import module[as a lias][,…]
from module import name[as a lias][,…]
from module import *
reload(module) ➤ module is reloaded (but existing references still refer old
module content)
Import can use package path (ex:from encoding.aliases import…).
Direct import from a package use definitions from __init__.py file.
Very careful with import* as imported names override names already
defined.
To limit your modules names exported and visible by import*, define
module global __all__ with list of exported names (or use global
names _xxx).
See __import__ builtin function, and modules imp, ihooks.
__import__(modulename[, globals[,locals[,lnameslist]]])
Source encodings
See PEP 263. Declare source files encoding in first or second line in a
special comment.
# -*- coding: encoding_name -*-
If this is not specified, Python defaultencoding() value
(see modules sitecustomize.py and user.py).
It is important to specify encoding of your modules as u"…" strings use
it to correctly build unicode literals.
Special Attributes
__name__ → str: module name, '__main__' for command-line called script
__file__ → string: pathname of compiled module loaded
M AIN E XECUTION / S CRIPT P ARAMETERS
The 'main' module is the module called via command-line (or executed
by shell with first script line #! /bin/env python).
Command-line parameters are available in sys.argv (a python list).
At end of module, we may have :
if __name__=='__main__' :
# main code
# generally call a 'main' function:
mainfunction(sys.argv[1:])
# or in lib modules, execute test/
Execution exit after last main module instruction (in multithread, wait
also for end of non-daemon threads), unless interactive mode is forced.
Can force exit with it(code), which raise a SystemExit exception - see Current Process - Exiting (p13).
O PERATORS
Deal with arithmetic, boolean logic, bit level, indexing and slicing.
Arithmetic Overriding
__add__(self,other) → value: called for self+other
__sub__(self,other) → value: called for self-other
__mul__(self,other) → value: called for self*other
__div__(self,other) → value: called1 for self/other
__truediv__(self,other) → value: called2 for self/other
__floordiv__(self,other) → value: called for self//other
__mod__(self,other) → value: called for self%other
__divmod__(self,other) → value: called for divmod(self,other)
__pow__(self,other) → value: called for self**other
__nonzero__(self)→ value: called for nonzero(self)
__neg__(self) → value: called for -self
__pos__(self) → value: called for +self
__abs__(self) → value: called for abs(self)
__iadd__(self,other) ➤ called for self+=other
__isub__(self,other) ➤ called for self-=other
__imul__(self,other) ➤ called for self*=other
__idiv__(self,other) ➤ called1 for self/=other
__itruediv__(self,other) ➤ called2 for self/=other
__ifloordiv__(self, other) ➤ called for self//=other
__imod__(self,other) ➤ called for self%=other
__ipow__(self,other) ➤ called for self**=other
1 without /
2 with from __futur__ import division
Binary operators __xxx__ have also __rxxx__ forms, called when target object is on right side.
Comparison Operators
Operators can compare any data types.
Compare values with <<=>>===!=<>.
Test objects identity with is and is not (compare on id(obj)).
Direct composition of comparators is allowed in expressions : x<y<=z>t. Builtin function cmp(o1,o2)→-1 (o1 < o2), 0 (o1 == o2), 1 (o1 > o2) Comparison Overriding
__lt__(self,other)→ bool1: called for self<other
__le__(self,other)→ bool1: called for self<=other
__gt__(self,other)→ bool1: called for self>other
__ge__(self,other)→ bool1: called for self>=other
__eq__(self,other)→ bool1: called for self==other
__ne__(self,other)→ bool1: called for self!=other
and for self<>other
__cmp__(self,other)→ int: called for self compared to other,
self<other→value<0, self==other→value=0, self>other→value>0
1 Any value usable as boolean value, or a NotImplemented value if cannot compare with such other type.
Operators as Functions
Operators are also defined as functions in standard operator module.
Comparison
lt(a,b) = __lt__(a,b)
le(a,b) = __le__(a,b)
eq(a,b) = __eq__(a,b)
ne(a,b) = __ne__(a,b)
ge(a,b) = __ge__(a,b)
gt(a,b) = __gt__(a,b)
Logical / Boolean
not_(o) = __not__(o)
truth(o)
is_(a,b)
is_not(a,b)
and_(a,b) = __and__(a,b)
or_(a,b) = __or__(a,b)
xor(a,b) = __xor__(a,b)
Arithmetic
abs(o) = __abs__(o)
add(a,b) = __add__(a,b)
sub(a,b) = __sub__(a,b)
mul(a,b) = __mul__(a,b)
div(a,b) = __div__(a,b)
mod(a,b) = __mod__(a,b)
truediv(a,b) = __truediv__(a,b)
floordiv(a,b) = __floordiv__(a,b)
neg(o) = __neg__(o)
pos(o) = __pos__(o)
pow(a,b) = __pow__(a,b)
Bit Level
lshift(a,b) = __lshift__(a,b)
rshift(a,b) = __rshift__(a,b)
inv(o) = invert(o) = __inv__(o) = __invert__(o)
Sequences
concat(a,b) = __concat__(a,b)
contains(a,b) = __contains__(a,b)
countOf(a,b)
indexOf(a,b)
repeat(a,b) = __repeat__(a,b)
setitem(a,b,c) = __setitem__(a,b,c)
getitem(a,b) = __getitem__(a,b)
delitem(a,b) = __delitem__(a,b)
setslice(a,b,c,v) = __setslice__(a,b,c,v)
getslice(a,b,c) = __getslice__(a,b,c)
delslice(a,b,c) = __delslice__(a,b,c)
Type Testing
These functions must be considered as not reliable.
isMappingType(o)
isNumberType(o)
isSequenceType(o)
Attribute and Item Lookup
attrgetter(attr) → fct: where fct(x)→x.attr
itemgetter(item) → fct: where fct(x)→x[item]
B OOLEANS
False : None, zero numbers, empty containers. False → 0.
True : if not false. True → 1.
bool(expr) → True | False
Logical not : not expr
Logical and : expr1and expr2
Logical or : expr1or expr2
Logical and and or use short path evaluation.
Bool Cast Overriding
__nonzero__(self) → bool: test object itself1
1 If __nonzero__ undefined, look at __len__, else object is true.
N UMBERS
Builtin integer types : int (like C long), long (unlimited integer)
int(expr[,base=10]) → int: cast of expr
long(expr[,base=10]) → long: cast of expr
Builtin floating point types : float (like C double), complex (real and
imaginary parts are float).
float(expr)→float: representation of expr
complex(x[,y]) →complex: number: x+yj
[x+]y j→complex: number, ex: 3+4j -8.2j
c.img→float: imaginary part of complex number
Maximum int integer in sys.maxint.
Automatic conversions between numeric types.
Automatic conversions from int to long when result overflow max int.
Direct conversions from/to strings from/to int, long… via types
constructors.
Type Decimal defined in standard module decimal.
Base fixed type compact storage arrays in standard module array.
Operators
-x+x x+y x-y x*y x/y 1x//y 1x%y 2x**y 2
1 With from__future__import division, / is true division (1/2→0.5),
and // is floor division (1//2→0). Else for integers / is still floor division.
2% is remainder operator, ** is power elevation operator (same as pow).
Functions
Some functions in builtins.
abs(x) → absolute value of x
divmod(x,y)→(x/y,x%y)
oct(integer) → str: octal representation of integer number
hex(integer) → str: hexadecimal representation of integer number
Representation formating functions in strings Formating (p5) and
Localization (p6).
Math Functions
Standard floating point functions/data in standard math module.
acos(x) → float: radians angle for x cosinus value : [-1…1] →[0…π]
asin(x) → float: radians angle for x sinus value : [-1…1] →[-π/2…+π/2]
atan(x) → float: radians angle for x tangent value : [-∞…∞] →]-π/2…+π/2[
atan2(x,y) → float: randians angle for x/y tangent value
ceil(x) → float: smallest integral value >= x
cos(x) → float: cosinus value for radians angle x
cosh(x) → float: hyperbolic cosinus value for radians angle x
exp(x) → float: exponential of x = e x
fabs(x) → float: absolute value of x
floor(x) → float: largest integral value <= x
fmod(x,y) → float: modulo = remainder of x/y
frexp(x) → (float,int): (m,y) m mantissa of x, y exponent of x — where
x=m*2y
ldepx(x,i)→ float: x multiplied by 2 raised to i power: x * 2i
log(x) → float: neperian logarithm of x
log10(x) → float: decimal logarithm of x
modf(x) → (float{2}): (f,i) f signed fractional part of x, i signed integer part of
x
pow(x,y) → float: x raised to y power (x y)
sin(x) → float: sinus value for radians angle x
sinh(x)→float: hyperbolic sinus value for radians angle x
sqrt(x) → float: square root of x (√x)
tan(x) → float: tangent value for radians angle x
tanh(x) →float: hyperbolic tangent value for radians angle x
pi → float: value of π (pi=3.1415926535897931)
e → float: value o
f neperian logarithms base (e=2.7182818284590451)
Module cmath provides similar functions for complex numbers.
Random Numbers
Randomization functions in standard random module. Module functions
use an hidden, shared state, Random type generator (uniform distribution).
Functions also available as methods of Random objects.
seed([x]) ➤ initialize random number generator
random()→ float: random value in [0.0, 1.0[
randint(a,b)→ int: random value in [a, b]
uniform(a, b)→ float: random value in [a, b[
getrandbits(k)→ long: with k random bits
randrange([start,]stop[,step])→ int: random value in range(start,stop, step)
choice(seq)→ value: random item from seq sequence
shuffle(x[,rndfct]) ➤ items of x randomly reordered using rndfct()
sample(population,k)→ list: k random items from polulation
Alternate random distributions : betavariate(alpha,beta), expovariate(lambd), gammavariate(alpha,beta), gauss(mu,sigma), lognormvariate(mu,sigma), normalvariate(mu,sigma), vonmisesvariate(mu,kappa), paretovariate(alpha),
weibullvariate(alpha,beta).
Alternate random generator WichmannHill class.
Direct generator manipulation : getstate(), setstate(state), jumpahead(n).
In module os, see :
os.urandom(n) → str: n random bytes suitable for cryptographic use Other Math Modules
Advanced matrix, algorithms and number crunching in third party modules like numpy (evolution of numarray / Numeric), gmpy (multiprecision arithmetic), DecInt, scipy, pyarray, …
See sites SciPy, BioPython, PyScience,…
Numbers Casts Overriding
__int__(self) → int: called for int(self)
__long__(self) → long: called for long(self)
__float__(self) → float: called for float(self)
__complex__(self) → complex: called for complex(self)
__oct__(self) → str: called for oct(self)
__hex__(self) → str: called for hex(self)
__coerce__(self,other) → value: called for coerce(self,other)
B IT L EVEL O PERATIONS
Work with int and long data.
Operators
~
x→ inverted bits of x
x^y→ bitwise exclusive or on x and y
x&y → bitwise and on x and y
x|y → bitwise or on x and y
x<<n→ x shifted left by n bits (zeroes inserted)
x>>n → x shifted right by n bits (zeroes inserted)
Binary structures manipulations in standard module struct. Advanced binary structures mapping and manipulation in third party modules : ctypes, xstruct, pyconstruct, …
Bit Level Overriding
__and__(self,other) → value: for self&other
__or__(self,other) → value: for self| other
__xor__(self,other) → value: for self^other
__lshift__(self,other) → value: for self<<other
__rshift__(self,other) → value: for self>>other
__invert__(self) → value: for ~self
__iand__(self,other) ➤ called for self&=other
__ior__(self,other) ➤ called for self|=other
__ixor__(self,other) ➤ called for self^=other
__ilshift__(self,other) ➤ called for self<<=other __irshift__(self,other) ➤ called for self>>=other
S TRINGS
Simple quoted 'Hello' or double-quoted "Hello".
Use triple [simple|double] quotes for multi-lines strings :
"""Hello,
how are you ?"""
Strings are immutable (once created a string cannot be modified in
place).
Strings can contain binary data, including null chars (chars of code 0).
Strings are sequences, see Indexing (p8)for chars indexation (slicing)
and other operations.
chr(code)→ str: string of one char
ord(char)→ int: code
str(expr)→ str: readable textual representation of expr - if available
`expr` → str: readable textual representation of expr - if available
repr(expr)→ str: evaluable textual representation of expr - if available
Escape sequences
\a - bell
\b - backspace
\e - escape
\f - form feed
\n - new line
\r - carriage return
\t - horizontal tab
\v - vertical tab
\' - single quote
\" - double quote
\\ - backslash
\ooo - char by octal ooo value
\x hh - char by hexadecimal hh value
\<newline> - continue string on next line.
And for Unicode strings :
\u xxxx- unicode char by 16 bits hexadecimal xxxx value.
\U xxxxxxxx - unicode char by 32 bits hexadecimal xxxxxxxx value.
\N{name} - unicode char by name in the Unicode database.
Keep \ escape chars by prefixing string literals with a r (or R) - for 'raw'
strings (note : cannot terminate a raw string with a \).
Unicode strings
Quoted as for str, but with a u (or U) prefix before the string : u"Voiçi"
U"""Une bonne journée
en perspective."""
Can mix strings prefixs r (or R) and u (or U).
You must define your source file encoding so that Python knows how to
convert your source literal strings into internal unicode strings.
unichr(code) → unicode: string of one char
ord(unicode char) → int: unicode code
unicode(object[,encoding[,errors]]) → unicode: unicode
sys.maxunicode → int: maximum unicode code=fct(compile time option)
Unicode Chars Informations
Module unicodedata contains informations about Unicode chars
properties, names.
lookup(name) → unicode: unicode char from its name
name(unichr[,default]) → str: unicode name - may raise ValueError
decimal(unichr[,default]) → int: decimal value - may raise ValueError
digit(unichr[,default]) → int: digit value - may raise ValueError
numeric(unichr[,default]) → float: numeric value - may raise ValueError
category(unichr) → str: general unicode category of char
bidirectional(unichr) → str: bidir category of char, may be empty string
combining(unichr) → str/0: canonical combining class of char as integer
east_asian_width(unichr) → str: east asian width
mirrored(unichr) → int: mirrored property in bidi text, 1 if mirrored else 0
decomposition(unichr) → str: decomposition mapping, may be empty str
normalize(form,unistr) → str: normal form of string - form in 'NFC',
'NFKC', 'NFD', 'NFKD'
unidata_version → str: version of Unicode database used
Methods and Functions
From builtins (see also oct and hex functions for integers to strings) :
len(s) → int: number of chars in the string
Most string methods are also available as functions in the standard
string module.
s.capitalize() → string with first char capitalized1
<(width[,fillchar]) → string centered
s.decode([encoding[,errors]]) → unicode: text decoded - see encodings
(p12)
s.find(sub[,start[,end]]) → int/-1: offset of sub
s.index(sub[,start[,end]]) → int: offset of sub - may raise ValueError
s.isalnum() → bool: non empty string with all alphanumeric chars1
s.isalpha() → bool: non empty string with all alphabetic chars1
s.isdigit() → bool: non empty string with all digit chars1
s.islower() → bool: non empty string with all lower chars1
s.isspace() → bool: non empty string with all space chars1
s.istitle() → bool: non empty string with titlecase words1
s.isupper() → bool: non empty string with all upper chars1
s.join(seq) → string: seq[0]+s+seq[1]+s+…+seq[n-1]
s.ljust(width[,fillchar]) → text string left aligned2
s.lower() → text string lowered1
s.lstrip([chars]) → string text with leading chars2 removed
s.rfind(sub[,start[,end]]) → int/-1: last offset of sub
s.rindex(sub[,start[end]])→ int: last offset of sub - may raise ValueError
s.rjust(width[,fillchar]) → string text right aligned2
s.rsplit([sep[,maxsplit]])→ [string]: rightmost words delim. by sep2
s.rstrip([chars]) → string with trailing chars2 removed
s.split([sep[,maxsplit]]) → [string]: words delimited by sep2
s.splitlines([keepends]) → [string]: list of text lines
s.startswith(suffix[,start[,end]]) → bool: test text begining
s.strip([chars]) → string text with leading+trailing chars2 removed
s.swapcase() → string with case switched1
s.title() → string with words capitalized1
s.upper() → string uppered1
s.zfill(witdh) → string: string prefixed with zeroes to match width
1 Locale dependant for 8 bits strings.
2 Default chars/separator/fillchar is space.
3 For str table must be a string of 256 chars - see string.maketrans().
For Unicode no deletechars, and table must be a map of unicode
ordinals to unicode ordinals.
Formating
Use % operator between format string and arguments : string%args
Formating string contains %[(name)][flag][width][.precision]code
If not use %(name)… → args = single value or tuple of values.
If use %(name)… → args = mapping with name as keys.
For mapping, args can be an object with __getitem__ method - see
Overriding Mapping Operations (p8).
Format char codes
d signed int. decimal : -324i signed int. decimal : -324
o unsigned octal : 774u unsigned decimal 6953
x unsigned hexa : f3a X unsigned hexa : F3A
e float. point exp. : -3.256e-12E float. point exp. : -3.256E-12
f float. point dec. : -0.0000032F float. point dec. : -0.0000032
g like e or f G like E or F
c character (1 char str or code)%%% → %
r object format like repr(object)s object format like str(object)
Templates
With string.Template objects. Use common $ syntax : $$ ➤ s ingle $ ;
$name or ${name} ➤ v alue for name.
tmpl=string.Template(template_string)
tmpl.substitute(mapping[,**kwargs]) → string: template filled
tmpl.safe_substitute(mapping[,**kwargs]) → string: template plate→ string
Can subclass Template to build your own templating (see doc, sources). See also modules formatter.
Wrapping
Module textwrap has a TextWrapper class and tool functions.
tw=textwrap.TextWrapper([…]) → new text wrapper using named params as corresponding attributes values
tw.width → int: max length of wrapped lines (default 70)
tw.initial_indent → string: prepend to first wrapped line (default '')
tw.subsequent_indent → string: prepend to other wrapped lines (default '') tw.fix_sentence_endings → bool: try to separate sentences by two spaces (default False)
tw.break_long_words → bool: break words longer than width (default True) tw.initial_indent → string: prepend to first wrapped line (default '')
tw.wrap(text) → [string]: list of text lines, each with max width length - no final newline
tw.fill(text) → string: whole text, lines wrapped using newlines
Two convenient functions use temporary TextWrapper, built using named parameters corresponding to attributes.
wrap(text[,width=70[,…]]) → [string]
fill(text[,width=70[,…]]) → string
dedent(text) → string: remove uniform whitespaces at beginning of text lines Constants
Standard module string provide several constants (do not modify, they are used in string manipulation functions) and some str functions are not available as methods.
ascii_letters → str: lowercase and uppercase chars
ascii_lowercase → str: lowercase a-z chars
ascii_uppercase → str: uppercase A-Z chars
digits → str: 0-9 decimal digit chars
hexdigits → str: 0-9a-fA-F hexadecimal digit chars
letters → str: lowercase and uppercase chars1
lowercase → str: lowercase a-z chars1
octdigits → str: 0-7 octal digit chars
punctuation → str: ascii chars considered as punctuation in C locale printable → str: printable chars
uppercase → str: uppercase A-Z chars1
whitespace → str: whitespace chars (spc, tab, cr, lf, ff, vt)
capwords(s) → str: split → capitalize → join
maketrans(from,to) → translation table usable anslate - from and to must have same length
1Definition is locale dependant.
Regular Expressions
Standard module re has a powerfull regexp engine. See regexp HOWTO at www.amk.ca/python/howto/regex/.
Use raw string r"…" notation.
See also external projects pyparsing, PLY (Python Lex-Yacc), tpg (Toy Parser Generator)…
Expressions
Metacharacters :. ^ $ * + ? { } [ ] \ | ( ), may use \ escape.
. ➤ match any character except a newline (including newline with DOTALL option)
^➤ match start of string (and start of lines with MULTILINE option)
$ ➤ match end of string (and end of lines with MULTILINE option)
expr* ➤ match 0 or more repetitions of expr (as much as possible)expr+ ➤ match 1 or more repetitions of expr (as much as possible)
expr? ➤ match 0 or 1 expr
expr*? ➤ match like expr* but as few as possible
expr+? ➤ match like expr+ but as few as possible
expr?? ➤ match like expr? but as few as possible
expr{m} ➤ match m repetitions of expr
expr{[m],[n]} ➤ match from m to n repetitions of expr, missing m default to 0
and missing n default to infinite
expr{[m],[n]}? ➤ match like expr{[m],[n]} but as few as possible
[set] ➤ match one char in the set defined by :
^ → at begining, invert set definition
x-y→ chars from x to y
\x → see Escape sequences for strings (p5)
\- , \] → chars - and ] (- and ] at the beginning match - and ] chars)
x→ char x (including other re metacharacters)
exprA|exprB ➤ match exprA or exprB, short path evaluation
(expr) ➤ match expr and build a numbered group
(?[i][L][m][s][u][x]) ➤ (at least one ot iLmsux char) group match empty
string, modify options flags for entire expression - see I L M S U X options
(?:expr) ➤ match expr but dont build a group
(?P<name>expr) ➤ match expr and build a group numbered and named (name
must be valid Python identifier)
(?P=name) ➤ match text matched by earlier group named name
(?#text) ➤ no match, text is just a comment
(?=expr) ➤ match if match expr but don't consume input
(?!expr) ➤ match if doesn't match expr but don't consume input
(?<=expr) ➤ match if current position is immediatly preceded by a match for
fixed length pattern expr
(?<!expr) ➤ match if current position is immediatly not preceded by a match
for fixed length pattern expr
(?(num/name)yesexpr[|noexpr]) ➤ try to match yesexpr if group num/name
exists, else try to match noexpr
Escape Sequences
\n\nn ➤ match3 group number n (nn) where first n≠0
\ooo\0o➤ match3 char with octal value ooo (0o)
\A ➤ match only at the start of the string
\b ➤ match3 empty string at beginning or end of a word1+2
\B ➤ match empty string not at beginning or end of a word1+2
\d ➤ match char class decimal digit [0-9]
\D ➤ match char class non-digit [^0-9]
\s ➤ match char class whitespace [ \t\n\r\f\v]
\S ➤ match char class non-whitespace [^ \t\n\r\f\v]
\w ➤ match char class alphanumeric [a-zA-Z0-9_]
\W ➤ match char class non-alphanumeric [^a-zA-Z0-9_]
\Z ➤ match end of string
\a \b \f \n \r \t \v \x \\ ➤ same as string escapes
\c ➤ for other c chars, match char c
1 Depends on UNICODE flag.
2 Depends on LOCALE flag.
3 When out of char class definition ([…])
Flag Options
IGNORECASE (I) : case insensitive expression - not locale dependant.
LOCALE (L) : make \w\W\b\B locale dependant.
MULTILINE (M) : ^ and $ match begining/end of string and lines. Else ^
and $ match only beginning and end of string.
DOTALL (S) : make . match any char including newline. Else newline
excluded.
UNICODE (U) : make \w\W\b\B unicode dependant.
VERBOSE (X) : ignore whitespaces and make # starting comments (except
when space and # are escaped or in char class).
Matching and Searching
Can use re functions, or compile expressions into SRE_Pattern objects
and use their methods.
See Flag Options supra for flags parameters.
search(pattern,string[,flags])→ MatchObject/None: scan throught string to
find substrings matching pattern
match(pattern,string[,flags])→ MatchObject/None: try to match string with
pattern
split(pattern,string[,maxsplit=0])→ [string]: split string by occurences of
pattern - if maxsplit specified, remainder is put in last item of list
findall(pattern,string[,flags])→ [string]/[(string)]: find non-overlapping
substrings matching pattern - eventually empty matchs - return list of tuples if
pattern has groups
finditer(pattern,string[,flags])→ iterator over [MatchObject] - same as
findall but with an iterator
sub(pattern,repl,string[,count=0])→ string: replace substrings matching
pattern by repl - repl as string can contain back references1 to identified
substring - repl as fct(MatchObject) return replacement string - pattern may
be RE_Pattern object
subn(pattern,repl,string[,count=0])→ (string,int): same as sub, 2nd item is
count of substitutions
escape(string)→ string: non-alphanumerics backslashed
If you need to reuse a pattern, compile it one time for all.
pat=repile(pattern[,flags])→ RE_Pattern object
pat.match(string[,pos[,endpos]]) → same as match function2
pat.search(string[,pos[,endpos]]) → same as search function2
pat.split(string[,maxsplit=0])→ same as split function2
pat.findall(string[,pos[,endpos]])→ same as findall function2
pat.finditer(string[,pos[,endpos]])→ same as finditer function2
pat.sub(repl,string[,count=0])→ same as sub function
pat.subn(pattern,repl,string[,count=0])→ same as subn function
pat.flags → int: flags used at compile time
pat.pattern → string: pattern used at compile time
Several functions/methods return MatchObject objects.
method) using match object groups values
or names
access to subgroups not in the match
of the match - default give access to subgroups not in the match
m.start([group=0])→ int: index of start of substring matched by group, -1 if
group exists but not in match
group exists but not in match
m.span([group=0])→ (int{2}): values of start and end methods for the group
m.pos → int: pos value of search/match method
m.lastindex → int/None: index of last matched capturing group
m.lastgroup → string/None: name of last matched capturng group
< → RE_Pattern: pattern used to produce match object
m.string → string: string used in match/search to produce match object
1 Back references extended to \g<groupnum> and \g<groupname>.
1 Using part of string between pos and endpos.
Group number 0 correspond to entire matching.
Localization
Standard module locale provide posix locale service (internationa-
lization).
setlocale(category[,locale]) → current/new settings: if locale specified (as
string or as tuple(language code, encoding)) then modify locale settings for
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论