Contents
Yes.
The pdb module is a simple but adequate console-mode debugger for Python. It ispart of the standard Python library, and isdocumentedintheLibraryReferenceManual. You can also write your own debugger by using the codefor pdb as an example.
The IDLE interactive development environment, which is part of the standardPython distribution (normally available as Tools/scripts/idle), includes agraphical debugger. There is documentation for the IDLE debugger athttp://www.python.org/idle/doc/idle2.html#Debugger.
PythonWin is a Python IDE that includes a GUI debugger based on pdb. ThePythonwin debugger colors breakpoints and has quite a few cool features such asdebugging non-Pythonwin programs. Pythonwin is available as part of thePythonfor Windows Extensions project andas a part of the ActivePython distribution (seehttp://www.activestate.com/Products/ActivePython/index.html).
Boa Constructor is an IDE and GUIbuilder that uses wxWidgets. It offers visual frame creation and manipulation,an object inspector, many views on the source like object browsers, inheritancehierarchies, doc string generated html documentation, an advanced debugger,integrated help, and Zope support.
Eric is an IDE built on PyQtand the Scintilla editing component.
Pydb is a version of the standard Python debugger pdb, modified for use with DDD(Data Display Debugger), a popular graphical debugger front end. Pydb can befound athttp://bashdb.sourceforge.net/pydb/ and DDD can be found athttp://www.gnu.org/software/ddd.
There are a number of commercial Python IDEs that include graphical debuggers.They include:
Yes.
PyChecker is a static analysis tool that finds bugs in Python source code andwarns about code complexity and style. You can get PyChecker fromhttp://pychecker.sf.net.
Pylint is another tool that checksif a module satisfies a coding standard, and also makes it possible to writeplug-ins to add a custom feature. In addition to the bug checking thatPyChecker performs, Pylint offers some additional features such as checking linelength, whether variable names are well-formed according to your codingstandard, whether declared interfaces are fully implemented, and more.http://www.logilab.org/card/pylint_manual provides a full list of Pylint’sfeatures.
You don’t need the ability to compile Python to C code if all you want is astand-alone program that users can download and run without having to installthe Python distribution first. There are a number of tools that determine theset of modules required by a program and bind these modules together with aPython binary to produce a single executable.
One is to use the freeze tool, which is included in the Python source tree asTools/freeze. It converts Python byte code to C arrays; a C compiler you canembed all your modules into a new program, which is then linked with thestandard Python modules.
It works by scanning your source recursively for import statements (in bothforms) and looking for the modules in the standard Python path as well as in thesource directory (for built-in modules). It then turns the bytecode for moduleswritten in Python into C code (array initializers that can be turned into codeobjects using the marshal module) and creates a custom-made config file thatonly contains those built-in modules which are actually used in the program. Itthen compiles the generated C code and links it with the rest of the Pythoninterpreter to form a self-contained binary which acts exactly like your script.
Obviously, freeze requires a C compiler. There are several other utilitieswhich don’t. One is Thomas Heller’s py2exe (Windows only) at
Another is Christian Tismer’sSQFREEZEwhich appends the byte code to a specially-prepared Python interpreter that canfind the byte code in the executable.
Other tools include Fredrik Lundh’sSqueeze and Anthony Tuininga’scx_Freeze.
Yes. The coding style required for standard library modules is documented asPEP 8.
It can be a surprise to get the UnboundLocalError in previously workingcode when it is modified by adding an assignment statement somewhere inthe body of a function.
This code:
>>>x=10>>>defbar():...print(x)>>>bar()10
works, but this code:
>>>x=10>>>deffoo():...print(x)...x+=1
results in an UnboundLocalError:
>>>foo()Traceback (most recent call last):...UnboundLocalError:local variable 'x' referenced before assignment
This is because when you make an assignment to a variable in a scope, thatvariable becomes local to that scope and shadows any similarly named variablein the outer scope. Since the last statement in foo assigns a new value tox, the compiler recognizes it as a local variable. Consequently when theearlierprint(x) attempts to print the uninitialized local variable andan error results.
In the example above you can access the outer scope variable by declaring itglobal:
>>>x=10>>>deffoobar():...globalx...print(x)...x+=1>>>foobar()10
This explicit declaration is required in order to remind you that (unlike thesuperficially analogous situation with class and instance variables) you areactually modifying the value of the variable in the outer scope:
>>>print(x)11
You can do a similar thing in a nested scope using thenonlocalkeyword:
>>>deffoo():...x=10...defbar():...nonlocalx...print(x)...x+=1...bar()...print(x)>>>foo()1011
In Python, variables that are only referenced inside a function are implicitlyglobal. If a variable is assigned a new value anywhere within the function’sbody, it’s assumed to be a local. If a variable is ever assigned a new valueinside the function, the variable is implicitly local, and you need toexplicitly declare it as ‘global’.
Though a bit surprising at first, a moment’s consideration explains this. Onone hand, requiringglobal for assigned variables provides a baragainst unintended side-effects. On the other hand, ifglobal was requiredfor all global references, you’d be usingglobal all the time. You’d haveto declare as global every reference to a built-in function or to a component ofan imported module. This clutter would defeat the usefulness of theglobaldeclaration for identifying side-effects.
Assume you use a for loop to define a few different lambdas (or even plainfunctions), e.g.:
>>>squares=[]>>>forxinrange(5):...squares.append(lambda:x**2)
This gives you a list that contains 5 lambdas that calculatex**2. Youmight expect that, when called, they would return, respectively,0,1,4,9, and16. However, when you actually try you will see thatthey all return16:
>>>squares[2]()16>>>squares[4]()16
This happens becausex is not local to the lambdas, but is defined inthe outer scope, and it is accessed when the lambda is called — not when itis defined. At the end of the loop, the value ofx is4, so all thefunctions now return4**2, i.e.16. You can also verify this bychanging the value ofx and see how the results of the lambdas change:
>>>x=8>>>squares[2]()64
In order to avoid this, you need to save the values in variables local to thelambdas, so that they don’t rely on the value of the globalx:
>>>squares=[]>>>forxinrange(5):...squares.append(lambdan=x:n**2)
Here,n=x creates a new variablen local to the lambda and computedwhen the lambda is defined so that it has the same value thatx had atthat point in the loop. This means that the value ofn will be0in the first lambda,1 in the second,2 in the third, and so on.Therefore each lambda will now return the correct result:
>>>squares[2]()4>>>squares[4]()16
Note that this behaviour is not peculiar to lambdas, but applies to regularfunctions too.
The canonical way to share information across modules within a single program isto create a special module (often called config or cfg). Just import the configmodule in all modules of your application; the module then becomes available asa global name. Because there is only one instance of each module, any changesmade to the module object get reflected everywhere. For example:
config.py:
x=0# Default value of the 'x' configuration setting
mod.py:
importconfigconfig.x=1
main.py:
importconfigimportmodprint(config.x)
Note that using a module is also the basis for implementing the Singleton designpattern, for the same reason.
In general, don’t usefrommodulenameimport*. Doing so clutters theimporter’s namespace. Some people avoid this idiom even with the few modulesthat were designed to be imported in this manner. Modules designed in thismanner includetkinter, andthreading.
Import modules at the top of a file. Doing so makes it clear what other modulesyour code requires and avoids questions of whether the module name is in scope.Using one import per line makes it easy to add and delete module imports, butusing multiple imports per line uses less screen space.
It’s good practice if you import modules in the following order:
Never use relative package imports. If you’re writing code that’s in thepackage.sub.m1 module and want to importpackage.sub.m2, do not justwritefrom.importm2, even though it’s legal. Writefrompackage.subimportm2 instead. SeePEP 328 for details.
It is sometimes necessary to move imports to a function or class to avoidproblems with circular imports. Gordon McMillan says:
Circular imports are fine where both modules use the “import <module>” formof import. They fail when the 2nd module wants to grab a name out of thefirst (“from module import name”) and the import is at the top level. That’sbecause names in the 1st are not yet available, because the first module isbusy importing the 2nd.
In this case, if the second module is only used in one function, then the importcan easily be moved into that function. By the time the import is called, thefirst module will have finished initializing, and the second module can do itsimport.
It may also be necessary to move imports out of the top level of code if some ofthe modules are platform-specific. In that case, it may not even be possible toimport all of the modules at the top of the file. In this case, importing thecorrect modules in the corresponding platform-specific code is a good option.
Only move imports into a local scope, such as inside a function definition, ifit’s necessary to solve a problem such as avoiding a circular import or aretrying to reduce the initialization time of a module. This technique isespecially helpful if many of the imports are unnecessary depending on how theprogram executes. You may also want to move imports into a function if themodules are only ever used in that function. Note that loading a module thefirst time may be expensive because of the one time initialization of themodule, but loading a module multiple times is virtually free, costing only acouple of dictionary lookups. Even if the module name has gone out of scope,the module is probably available insys.modules.
If only instances of a specific class use a module, then it is reasonable toimport the module in the class’s__init__ method and then assign the moduleto an instance variable so that the module is always available (via thatinstance variable) during the life of the object. Note that to delay an importuntil the class is instantiated, the import must be inside a method. Puttingthe import inside the class but outside of any method still causes the import tooccur when the module is initialized.
Collect the arguments using the* and** specifiers in the function’sparameter list; this gives you the positional arguments as a tuple and thekeyword arguments as a dictionary. You can then pass these arguments whencalling another function by using* and**:
deff(x,*args,**kwargs):...kwargs['width']='14.3c'...g(x,*args,**kwargs)
Parameters are defined by the names that appear in afunction definition, whereasarguments are the valuesactually passed to a function when calling it. Parameters define what types ofarguments a function can accept. For example, given the function definition:
deffunc(foo,bar=None,**kwargs):pass
foo,bar andkwargs are parameters offunc. However, when callingfunc, for example:
func(42,bar=314,extra=somevar)
the values42,314, andsomevar are arguments.
Remember that arguments are passed by assignment in Python. Since assignmentjust creates references to objects, there’s no alias between an argument name inthe caller and callee, and so no call-by-reference per se. You can achieve thedesired effect in a number of ways.
By returning a tuple of the results:
deffunc2(a,b):a='new-value'# a and b are local namesb=b+1# assigned to new objectsreturna,b# return new valuesx,y='old-value',99x,y=func2(x,y)print(x,y)# output: new-value 100
This is almost always the clearest solution.
By using global variables. This isn’t thread-safe, and is not recommended.
By passing a mutable (changeable in-place) object:
deffunc1(a):a[0]='new-value'# 'a' references a mutable lista[1]=a[1]+1# changes a shared objectargs=['old-value',99]func1(args)print(args[0],args[1])# output: new-value 100
By passing in a dictionary that gets mutated:
deffunc3(args):args['a']='new-value'# args is a mutable dictionaryargs['b']=args['b']+1# change it in-placeargs={'a':' old-value','b':99}func3(args)print(args['a'],args['b'])
Or bundle up values in a class instance:
classcallByRef:def__init__(self,**args):for(key,value)inargs.items():setattr(self,key,value)deffunc4(args):args.a='new-value'# args is a mutable callByRefargs.b=args.b+1# change object in-placeargs=callByRef(a='old-value',b=99)func4(args)print(args.a,args.b)
There’s almost never a good reason to get this complicated.
Your best choice is to return a tuple containing the multiple results.
You have two choices: you can use nested scopes or you can use callable objects.For example, suppose you wanted to definelinear(a,b) which returns afunctionf(x) that computes the valuea*x+b. Using nested scopes:
deflinear(a,b):defresult(x):returna*x+breturnresult
Or using a callable object:
classlinear:def__init__(self,a,b):self.a,self.b=a,bdef__call__(self,x):returnself.a*x+self.b
In both cases,
taxes=linear(0.3,2)
gives a callable object wheretaxes(10e6)==0.3*10e6+2.
The callable object approach has the disadvantage that it is a bit slower andresults in slightly longer code. However, note that a collection of callablescan share their signature via inheritance:
classexponential(linear):# __init__ inheriteddef__call__(self,x):returnself.a*(x**self.b)
Object can encapsulate state for several methods:
classcounter:value=0defset(self,x):self.value=xdefup(self):self.value=self.value+1defdown(self):self.value=self.value-1count=counter()inc,dec,reset=count.up,count.down,count.set
Hereinc(),dec() andreset() act like functions which share thesame counting variable.
In general, trycopy.copy() orcopy.deepcopy() for the general case.Not all objects can be copied, but most can.
Some objects can be copied more easily. Dictionaries have acopy()method:
newdict=olddict.copy()
Sequences can be copied by slicing:
new_l=l[:]
For an instance x of a user-defined class,dir(x) returns an alphabetizedlist of the names containing the instance attributes and methods and attributesdefined by its class.
Generally speaking, it can’t, because objects don’t really have names.Essentially, assignment always binds a name to a value; The same is true ofdef andclass statements, but in that case the value is acallable. Consider the following code:
classA:passB=Aa=B()b=aprint(b)<__main__.Aobjectat0x16D07CC>print(a)<__main__.Aobjectat0x16D07CC>
Arguably the class has a name: even though it is bound to two names and invokedthrough the name B the created instance is still reported as an instance ofclass A. However, it is impossible to say whether the instance’s name is a orb, since both names are bound to the same value.
Generally speaking it should not be necessary for your code to “know the names”of particular values. Unless you are deliberately writing introspectiveprograms, this is usually an indication that a change of approach might bebeneficial.
In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer tothis question:
The same way as you get the name of that cat you found on your porch: the cat(object) itself cannot tell you its name, and it doesn’t really care – sothe only way to find out what it’s called is to ask all your neighbours(namespaces) if it’s their cat (object)...
....and don’t be surprised if you’ll find that it’s known by many names, orno name at all!
Comma is not an operator in Python. Consider this session:
>>>"a"in"b","a"(False, 'a')
Since the comma is not an operator, but a separator between expressions theabove is evaluated as if you had entered:
("a"in"b"),"a"
not:
"a"in("b","a")
The same is true of the various assignment operators (=,+= etc). Theyare not truly operators but syntactic delimiters in assignment statements.
Yes, there is. The syntax is as follows:
[on_true]if[expression]else[on_false]x,y=50,25small=xifx<yelsey
Before this syntax was introduced in Python 2.5, a common idiom was to uselogical operators:
[expression]and[on_true]or[on_false]
However, this idiom is unsafe, as it can give wrong results whenon_truehas a false boolean value. Therefore, it is always better to usethe...if...else... form.
Yes. Usually this is done by nestinglambda withinlambda. See the following three examples, due to Ulf Bartelt:
fromfunctoolsimportreduce# Primes < 1000print(list(filter(None,map(lambday:y*reduce(lambdax,y:x*y!=0,map(lambdax,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))))# First 10 Fibonacci numbersprint(list(map(lambdax,f=lambdax,f:(f(x-1,f)+f(x-2,f))ifx>1else1:f(x,f),range(10))))# Mandelbrot setprint((lambdaRu,Ro,Iu,Io,IM,Sx,Sy:reduce(lambdax,y:x+y,map(lambday,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambdayc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,Sx=Sx,Sy=Sy:reduce(lambdax,y:x+y,map(lambdax,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,i=i,Sx=Sx,F=lambdaxc,yc,x,y,k,f=lambdaxc,yc,x,y,k,f:(k<=0)or(x*x+y*y>=4.0)or1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy))))(-2.1,0.7,-1.2,1.2,30,80,24))# \___ ___/ \___ ___/ | | |__ lines on screen# V V | |______ columns on screen# | | |__________ maximum of "iterations"# | |_________________ range on y axis# |____________________________ range on x axis
Don’t try this at home, kids!
To specify an octal digit, precede the octal value with a zero, and then a loweror uppercase “o”. For example, to set the variable “a” to the octal value “10”(8 in decimal), type:
>>>a=0o10>>>a8
Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero,and then a lower or uppercase “x”. Hexadecimal digits can be specified in loweror uppercase. For example, in the Python interpreter:
>>>a=0xa5>>>a165>>>b=0XB2>>>b178
It’s primarily driven by the desire thati%j have the same sign asj.If you want that, and also want:
i==(i//j)*j+(i%j)
then integer division has to return the floor. C also requires that identity tohold, and then compilers that truncatei//j need to makei%j havethe same sign asi.
There are few real use cases fori%j whenj is negative. Whenjis positive, there are many, and in virtually all of them it’s more useful fori%j to be>=0. If the clock says 10 now, what did it say 200 hoursago?-190%12==2 is useful;-190%12==-10 is a bug waiting tobite.
For integers, use the built-inint() type constructor, e.g.int('144')==144. Similarly,float() converts to floating-point,e.g.float('144')==144.0.
By default, these interpret the number as decimal, so thatint('0144')==144 andint('0x144') raisesValueError.int(string,base) takesthe base to convert from as a second optional argument, soint('0x144',16)==324. If the base is specified as 0, the number is interpreted using Python’srules: a leading ‘0’ indicates octal, and ‘0x’ indicates a hex number.
Do not use the built-in functioneval() if all you need is to convertstrings to numbers.eval() will be significantly slower and it presents asecurity risk: someone could pass you a Python expression that might haveunwanted side effects. For example, someone could pass__import__('os').system("rm-rf$HOME") which would erase your homedirectory.
eval() also has the effect of interpreting numbers as Python expressions,so that e.g.eval('09') gives a syntax error because Python does not allowleading ‘0’ in a decimal number (except ‘0’).
To convert, e.g., the number 144 to the string ‘144’, use the built-in typeconstructorstr(). If you want a hexadecimal or octal representation, usethe built-in functionshex() oroct(). For fancy formatting, seetheString Formatting section, e.g."{:04d}".format(144) yields'0144' and"{:.3f}".format(1/3) yields'0.333'.
You can’t, because strings are immutable. In most situations, you shouldsimply construct a new string from the various parts you want to assembleit from. However, if you need an object with the ability to modify in-placeunicode data, try using aio.StringIO object or thearraymodule:
>>>importio>>>s="Hello, world">>>sio=io.StringIO(s)>>>sio.getvalue()'Hello, world'>>>sio.seek(7)7>>>sio.write("there!")6>>>sio.getvalue()'Hello, there!'>>>importarray>>>a=array.array('u',s)>>>print(a)array('u', 'Hello, world')>>>a[0]='y'>>>print(a)array('u', 'yello, world')>>>a.tounicode()'yello, world'
There are various techniques.
The best is to use a dictionary that maps strings to functions. The primaryadvantage of this technique is that the strings do not need to match the namesof the functions. This is also the primary technique used to emulate a caseconstruct:
defa():passdefb():passdispatch={'go':a,'stop':b}# Note lack of parens for funcsdispatch[get_input()]()# Note trailing parens to call function
Use the built-in functiongetattr():
importfoogetattr(foo,'bar')()
Note thatgetattr() works on any object, including classes, classinstances, modules, and so on.
This is used in several places in the standard library, like this:
classFoo:defdo_foo(self):...defdo_bar(self):...f=getattr(foo_instance,'do_'+opname)f()
Uselocals() oreval() to resolve the function name:
defmyFunc():print("hello")fname="myFunc"f=locals()[fname]f()f=eval(fname)f()
Note: Usingeval() is slow and dangerous. If you don’t have absolutecontrol over the contents of the string, someone could pass a string thatresulted in an arbitrary function being executed.
You can useS.rstrip("\r\n") to remove all occurrences of any lineterminator from the end of the stringS without removing other trailingwhitespace. If the stringS represents more than one line, with severalempty lines at the end, the line terminators for all the blank lines willbe removed:
>>>lines=("line 1\r\n"..."\r\n"..."\r\n")>>>lines.rstrip("\n\r")'line 1 '
Since this is typically only desired when reading text one line at a time, usingS.rstrip() this way works well.
Not as such.
For simple input parsing, the easiest approach is usually to split the line intowhitespace-delimited words using thesplit() method of string objectsand then convert decimal strings to numeric values usingint() orfloat().split() supports an optional “sep” parameter which is usefulif the line uses something other than whitespace as a separator.
For more complicated input parsing, regular expressions are more powerfulthan C’ssscanf() and better suited for the task.
That’s a tough one, in general. First, here are a list of things toremember before diving further:
That being said, there are many tricks to speed up Python code. Here aresome general principles which go a long way towards reaching acceptableperformance levels:
If you have reached the limit of what pure Python can allow, there are toolsto take you further away. For example,Cython cancompile a slightly modified version of Python code into a C extension, andcan be used on many different platforms. Cython can take advantage ofcompilation (and optional type annotations) to make your code significantlyfaster than when interpreted. If you are confident in your C programmingskills, you can alsowrite a C extension moduleyourself.
See also
The wiki page devoted toperformance tips.
str andbytes objects are immutable, therefore concatenatingmany strings together is inefficient as each concatenation creates a newobject. In the general case, the total runtime cost is quadratic in thetotal string length.
To accumulate manystr objects, the recommended idiom is to placethem into a list and callstr.join() at the end:
chunks=[]forsinmy_strings:chunks.append(s)result=''.join(chunks)
(another reasonably efficient idiom is to useio.StringIO)
To accumulate manybytes objects, the recommended idiom is to extendabytearray object using in-place concatenation (the+= operator):
result=bytearray()forbinmy_bytes_objects:result+=b
The type constructortuple(seq) converts any sequence (actually, anyiterable) into a tuple with the same items in the same order.
For example,tuple([1,2,3]) yields(1,2,3) andtuple('abc')yields('a','b','c'). If the argument is a tuple, it does not make a copybut returns the same object, so it is cheap to calltuple() when youaren’t sure that an object is already a tuple.
The type constructorlist(seq) converts any sequence or iterable into a listwith the same items in the same order. For example,list((1,2,3)) yields[1,2,3] andlist('abc') yields['a','b','c']. If the argumentis a list, it makes a copy just likeseq[:] would.
Python sequences are indexed with positive numbers and negative numbers. Forpositive numbers 0 is the first index 1 is the second index and so forth. Fornegative indices -1 is the last index and -2 is the penultimate (next to last)index and so forth. Think ofseq[-n] as the same asseq[len(seq)-n].
Using negative indices can be very convenient. For exampleS[:-1] is all ofthe string except for its last character, which is useful for removing thetrailing newline from a string.
Use thereversed() built-in function, which is new in Python 2.4:
forxinreversed(sequence):...# do something with x...
This won’t touch your original sequence, but build a new copy with reversedorder to iterate over.
With Python 2.3, you can use an extended slice syntax:
forxinsequence[::-1]:...# do something with x...
See the Python Cookbook for a long discussion of many ways to do this:
If you don’t mind reordering the list, sort it and then scan from the end of thelist, deleting duplicates as you go:
ifmylist:mylist.sort()last=mylist[-1]foriinrange(len(mylist)-2,-1,-1):iflast==mylist[i]:delmylist[i]else:last=mylist[i]
If all elements of the list may be used as set keys (i.e. they are allhashable) this is often faster
mylist=list(set(mylist))
This converts the list into a set, thereby removing duplicates, and then backinto a list.
Use a list:
["this",1,"is","an","array"]
Lists are equivalent to C or Pascal arrays in their time complexity; the primarydifference is that a Python list can contain objects of many different types.
Thearray module also provides methods for creating arrays of fixed typeswith compact representations, but they are slower to index than lists. Alsonote that the Numeric extensions and others define array-like structures withvarious characteristics as well.
To get Lisp-style linked lists, you can emulate cons cells using tuples:
lisp_list=("like",("this",("example",None)))
If mutability is desired, you could use lists instead of tuples. Here theanalogue of lisp car islisp_list[0] and the analogue of cdr islisp_list[1]. Only do this if you’re sure you really need to, because it’susually a lot slower than using Python lists.
You probably tried to make a multidimensional array like this:
>>>A=[[None]*2]*3
This looks correct if you print it:
>>>A[[None, None], [None, None], [None, None]]
But when you assign a value, it shows up in multiple places:
>>>A[0][0]=5>>>A[[5, None], [5, None], [5, None]]
The reason is that replicating a list with* doesn’t create copies, it onlycreates references to the existing objects. The*3 creates a listcontaining 3 references to the same list of length two. Changes to one row willshow in all rows, which is almost certainly not what you want.
The suggested approach is to create a list of the desired length first and thenfill in each element with a newly created list:
A=[None]*3foriinrange(3):A[i]=[None]*2
This generates a list containing 3 different lists of length two. You can alsouse a list comprehension:
w,h=2,3A=[[None]*wforiinrange(h)]
Or, you can use an extension that provides a matrix datatype;Numeric Python is the best known.
Use a list comprehension:
result=[obj.method()forobjinmylist]
This is because of a combination of the fact that augmented assignmentoperators areassignment operators, and the difference between mutable andimmutable objects in Python.
This discussion applies in general when augmented assignment operators areapplied to elements of a tuple that point to mutable objects, but we’ll usealist and+= as our exemplar.
If you wrote:
>>>a_tuple=(1,2)>>>a_tuple[0]+=1Traceback (most recent call last):...TypeError:'tuple' object does not support item assignment
The reason for the exception should be immediately clear:1 is added to theobjecta_tuple[0] points to (1), producing the result object,2,but when we attempt to assign the result of the computation,2, to element0 of the tuple, we get an error because we can’t change what an element ofa tuple points to.
Under the covers, what this augmented assignment statement is doing isapproximately this:
>>>result=a_tuple[0]+1>>>a_tuple[0]=resultTraceback (most recent call last):...TypeError:'tuple' object does not support item assignment
It is the assignment part of the operation that produces the error, since atuple is immutable.
When you write something like:
>>>a_tuple=(['foo'],'bar')>>>a_tuple[0]+=['item']Traceback (most recent call last):...TypeError:'tuple' object does not support item assignment
The exception is a bit more surprising, and even more surprising is the factthat even though there was an error, the append worked:
>>>a_tuple[0]['foo', 'item']
To see why this happens, you need to know that (a) if an object implements an__iadd__ magic method, it gets called when the+= augmented assignmentis executed, and its return value is what gets used in the assignment statement;and (b) for lists,__iadd__ is equivalent to callingextend on the listand returning the list. That’s why we say that for lists,+= is a“shorthand” forlist.extend:
>>>a_list=[]>>>a_list+=[1]>>>a_list[1]
This is equivalent to:
>>>result=a_list.__iadd__([1])>>>a_list=result
The object pointed to by a_list has been mutated, and the pointer to themutated object is assigned back toa_list. The end result of theassignment is a no-op, since it is a pointer to the same object thata_listwas previously pointing to, but the assignment still happens.
Thus, in our tuple example what is happening is equivalent to:
>>>result=a_tuple[0].__iadd__(['item'])>>>a_tuple[0]=resultTraceback (most recent call last):...TypeError:'tuple' object does not support item assignment
The__iadd__ succeeds, and thus the list is extended, but even thoughresult points to the same object thata_tuple[0] already points to,that final assignment still results in an error, because tuples are immutable.
The technique, attributed to Randal Schwartz of the Perl community, sorts theelements of a list by a metric which maps each element to its “sort value”. InPython, just use thekey argument for thesort() method:
Isorted=L[:]Isorted.sort(key=lambdas:int(s[10:15]))
Thekey argument is new in Python 2.4, for older versions this kind ofsorting is quite simple to do with list comprehensions. To sort a list ofstrings by their uppercase values:
tmp1=[(x.upper(),x)forxinL]# Schwartzian transformtmp1.sort()Usorted=[x[1]forxintmp1]
To sort by the integer value of a subfield extending from positions 10-15 ineach string:
tmp2=[(int(s[10:15]),s)forsinL]# Schwartzian transformtmp2.sort()Isorted=[x[1]forxintmp2]
For versions prior to 3.0, Isorted may also be computed by
defintfield(s):returnint(s[10:15])defIcmp(s1,s2):returncmp(intfield(s1),intfield(s2))Isorted=L[:]Isorted.sort(Icmp)
but since this method callsintfield() many times for each element of L, itis slower than the Schwartzian Transform.
Merge them into an iterator of tuples, sort the resulting list, and then pickout the element you want.
>>>list1=["what","I'm","sorting","by"]>>>list2=["something","else","to","sort"]>>>pairs=zip(list1,list2)>>>pairs=sorted(pairs)>>>pairs[("I'm", 'else'), ('by', 'sort'), ('sorting', 'to'), ('what', 'something')]>>>result=[x[1]forxinpairs]>>>result['else', 'sort', 'to', 'something']
An alternative for the last step is:
>>>result=[]>>>forpinpairs:result.append(p[1])
If you find this more legible, you might prefer to use this instead of the finallist comprehension. However, it is almost twice as slow for long lists. Why?First, theappend() operation has to reallocate memory, and while it usessome tricks to avoid doing that each time, it still has to do it occasionally,and that costs quite a bit. Second, the expression “result.append” requires anextra attribute lookup, and third, there’s a speed reduction from having to makeall those function calls.
A class is the particular object type created by executing a class statement.Class objects are used as templates to create instance objects, which embodyboth the data (attributes) and code (methods) specific to a datatype.
A class can be based on one or more other classes, called its base class(es). Itthen inherits the attributes and methods of its base classes. This allows anobject model to be successively refined by inheritance. You might have agenericMailbox class that provides basic accessor methods for a mailbox,and subclasses such asMboxMailbox,MaildirMailbox,OutlookMailboxthat handle various specific mailbox formats.
A method is a function on some objectx that you normally call asx.name(arguments...). Methods are defined as functions inside the classdefinition:
classC:defmeth(self,arg):returnarg*2+self.attribute
Self is merely a conventional name for the first argument of a method. A methoddefined asmeth(self,a,b,c) should be called asx.meth(a,b,c) forsome instancex of the class in which the definition occurs; the calledmethod will think it is called asmeth(x,a,b,c).
See alsoWhy must ‘self’ be used explicitly in method definitions and calls?.
Use the built-in functionisinstance(obj,cls). You can check if an objectis an instance of any of a number of classes by providing a tuple instead of asingle class, e.g.isinstance(obj,(class1,class2,...)), and can alsocheck whether an object is one of Python’s built-in types, e.g.isinstance(obj,str) orisinstance(obj,(int,float,complex)).
Note that most programs do not useisinstance() on user-defined classesvery often. If you are developing the classes yourself, a more properobject-oriented style is to define methods on the classes that encapsulate aparticular behaviour, instead of checking the object’s class and doing adifferent thing based on what class it is. For example, if you have a functionthat does something:
defsearch(obj):ifisinstance(obj,Mailbox):# ... code to search a mailboxelifisinstance(obj,Document):# ... code to search a documentelif...
A better approach is to define asearch() method on all the classes and justcall it:
classMailbox:defsearch(self):# ... code to search a mailboxclassDocument:defsearch(self):# ... code to search a documentobj.search()
Delegation is an object oriented technique (also called a design pattern).Let’s say you have an objectx and want to change the behaviour of just oneof its methods. You can create a new class that provides a new implementationof the method you’re interested in changing and delegates all other methods tothe corresponding method ofx.
Python programmers can easily implement delegation. For example, the followingclass implements a class that behaves like a file but converts all written datato uppercase:
classUpperOut:def__init__(self,outfile):self._outfile=outfiledefwrite(self,s):self._outfile.write(s.upper())def__getattr__(self,name):returngetattr(self._outfile,name)
Here theUpperOut class redefines thewrite() method to convert theargument string to uppercase before calling the underlyingself.__outfile.write() method. All other methods are delegated to theunderlyingself.__outfile object. The delegation is accomplished via the__getattr__ method; consultthe language referencefor more information about controlling attribute access.
Note that for more general cases delegation can get trickier. When attributesmust be set as well as retrieved, the class must define a__setattr__()method too, and it must do so carefully. The basic implementation of__setattr__() is roughly equivalent to the following:
classX:...def__setattr__(self,name,value):self.__dict__[name]=value...
Most__setattr__() implementations must modifyself.__dict__ to storelocal state for self without causing an infinite recursion.
Use the built-insuper() function:
classDerived(Base):defmeth(self):super(Derived,self).meth()
For version prior to 3.0, you may be using classic classes: For a classdefinition such asclassDerived(Base):... you can call methodmeth()defined inBase (or one ofBase‘s base classes) asBase.meth(self,arguments...). Here,Base.meth is an unbound method, so you need toprovide theself argument.
You could define an alias for the base class, assign the real base class to itbefore your class definition, and use the alias throughout your class. Then allyou have to change is the value assigned to the alias. Incidentally, this trickis also handy if you want to decide dynamically (e.g. depending on availabilityof resources) which base class to use. Example:
BaseAlias=<realbaseclass>classDerived(BaseAlias):defmeth(self):BaseAlias.meth(self)...
Both static data and static methods (in the sense of C++ or Java) are supportedin Python.
For static data, simply define a class attribute. To assign a new value to theattribute, you have to explicitly use the class name in the assignment:
classC:count=0# number of times C.__init__ calleddef__init__(self):C.count=C.count+1defgetcount(self):returnC.count# or return self.count
c.count also refers toC.count for anyc such thatisinstance(c,C) holds, unless overridden byc itself or by some class on the base-classsearch path fromc.__class__ back toC.
Caution: within a method of C, an assignment likeself.count=42 creates anew and unrelated instance named “count” inself‘s own dict. Rebinding of aclass-static data name must always specify the class whether inside a method ornot:
C.count=314
Static methods are possible:
classC:@staticmethoddefstatic(arg1,arg2,arg3):# No 'self' parameter!...
However, a far more straightforward way to get the effect of a static method isvia a simple module-level function:
defgetcount():returnC.count
If your code is structured so as to define one class (or tightly related classhierarchy) per module, this supplies the desired encapsulation.
This answer actually applies to all methods, but the question usually comes upfirst in the context of constructors.
In C++ you’d write
classC{C(){cout<<"No arguments\n";}C(inti){cout<<"Argument is "<<i<<"\n";}}
In Python you have to write a single constructor that catches all cases usingdefault arguments. For example:
classC:def__init__(self,i=None):ifiisNone:print("No arguments")else:print("Argument is",i)
This is not entirely equivalent, but close enough in practice.
You could also try a variable-length argument list, e.g.
def__init__(self,*args):...
The same approach works for all method definitions.
Variable names with double leading underscores are “mangled” to provide a simplebut effective way to define class private variables. Any identifier of the form__spam (at least two leading underscores, at most one trailing underscore)is textually replaced with_classname__spam, whereclassname is thecurrent class name with any leading underscores stripped.
This doesn’t guarantee privacy: an outside user can still deliberately accessthe “_classname__spam” attribute, and private values are visible in the object’s__dict__. Many Python programmers never bother to use private variablenames at all.
There are several possible reasons for this.
The del statement does not necessarily call__del__() – it simplydecrements the object’s reference count, and if this reaches zero__del__() is called.
If your data structures contain circular links (e.g. a tree where each child hasa parent reference and each parent has a list of children) the reference countswill never go back to zero. Once in a while Python runs an algorithm to detectsuch cycles, but the garbage collector might run some time after the lastreference to your data structure vanishes, so your__del__() method may becalled at an inconvenient and random time. This is inconvenient if you’re tryingto reproduce a problem. Worse, the order in which object’s__del__()methods are executed is arbitrary. You can rungc.collect() to force acollection, but thereare pathological cases where objects will never becollected.
Despite the cycle collector, it’s still a good idea to define an explicitclose() method on objects to be called whenever you’re done with them. Theclose() method can then remove attributes that refer to subobjecs. Don’tcall__del__() directly –__del__() should callclose() andclose() should make sure that it can be called more than once for the sameobject.
Another way to avoid cyclical references is to use theweakref module,which allows you to point to objects without incrementing their reference count.Tree data structures, for instance, should use weak references for their parentand sibling references (if they need them!).
Finally, if your__del__() method raises an exception, a warning messageis printed tosys.stderr.
Python does not keep track of all instances of a class (or of a built-in type).You can program the class’s constructor to keep track of all instances bykeeping a list of weak references to each instance.
Theid() builtin returns an integer that is guaranteed to be unique duringthe lifetime of the object. Since in CPython, this is the object’s memoryaddress, it happens frequently that after an object is deleted from memory, thenext freshly created object is allocated at the same position in memory. Thisis illustrated by this example:
>>>id(1000)13901272>>>id(2000)13901272
The two ids belong to different integer objects that are created before, anddeleted immediately after execution of theid() call. To be sure thatobjects whose id you want to examine are still alive, create another referenceto the object:
>>>a=1000;b=2000>>>id(a)13901272>>>id(b)13891296
When a module is imported for the first time (or when the source file haschanged since the current compiled file was created) a.pyc file containingthe compiled code should be created in a__pycache__ subdirectory of thedirectory containing the.py file. The.pyc file will have afilename that starts with the same name as the.py file, and ends with.pyc, with a middle component that depends on the particularpythonbinary that created it. (SeePEP 3147 for details.)
One reason that a.pyc file may not be created is a permissions problemwith the directory containing the source file, meaning that the__pycache__subdirectory cannot be created. This can happen, for example, if you develop asone user but run as another, such as if you are testing with a web server.
Unless thePYTHONDONTWRITEBYTECODE environment variable is set,creation of a .pyc file is automatic if you’re importing a module and Pythonhas the ability (permissions, free space, etc...) to create a__pycache__subdirectory and write the compiled module to that subdirectory.
Running Python on a top level script is not considered an import and no.pyc will be created. For example, if you have a top-level modulefoo.py that imports another modulexyz.py, when you runfoo (bytypingpythonfoo.py as a shell command), a.pyc will be created forxyz becausexyz is imported, but no.pyc file will be created forfoo sincefoo.py isn’t being imported.
If you need to create a.pyc file forfoo – that is, to create a.pyc file for a module that is not imported – you can, using thepy_compile andcompileall modules.
Thepy_compile module can manually compile any module. One way is to usethecompile() function in that module interactively:
>>>importpy_compile>>>py_compile.compile('foo.py')
This will write the.pyc to a__pycache__ subdirectory in the samelocation asfoo.py (or you can override that with the optional parametercfile).
You can also automatically compile all files in a directory or directories usingthecompileall module. You can do it from the shell prompt by runningcompileall.py and providing the path of a directory containing Python filesto compile:
python-mcompileall.
A module can find out its own module name by looking at the predefined globalvariable__name__. If this has the value'__main__', the program isrunning as a script. Many modules that are usually used by importing them alsoprovide a command-line interface or a self-test, and only execute this codeafter checking__name__:
defmain():print('Running test...')...if__name__=='__main__':main()
Suppose you have the following modules:
foo.py:
frombarimportbar_varfoo_var=1
bar.py:
fromfooimportfoo_varbar_var=2
The problem is that the interpreter will perform the following steps:
The last step fails, because Python isn’t done with interpretingfoo yet andthe global symbol dictionary forfoo is still empty.
The same thing happens when you useimportfoo, and then try to accessfoo.foo_var in global code.
There are (at least) three possible workarounds for this problem.
Guido van Rossum recommends avoiding all uses offrom<module>import...,and placing all code inside functions. Initializations of global variables andclass variables should use constants or built-in functions only. This meanseverything from an imported module is referenced as<module>.<name>.
Jim Roskind suggests performing steps in the following order in each module:
van Rossum doesn’t like this approach much because the imports appear in astrange place, but it does work.
Matthias Urlichs recommends restructuring your code so that the recursive importis not necessary in the first place.
These solutions are not mutually exclusive.
Try:
__import__('x.y.z').y.z
For more realistic situations, you may have to do something like
m=__import__(s)foriins.split(".")[1:]:m=getattr(m,i)
Seeimportlib for a convenience function calledimport_module().
For reasons of efficiency as well as consistency, Python only reads the modulefile on the first time a module is imported. If it didn’t, in a programconsisting of many modules where each one imports the same basic module, thebasic module would be parsed and re-parsed many times. To force rereading of achanged module, do this:
importimpimportmodnameimp.reload(modname)
Warning: this technique is not 100% fool-proof. In particular, modulescontaining statements like
frommodnameimportsome_objects
will continue to work with the old version of the imported objects. If themodule contains class definitions, existing class instances willnot beupdated to use the new class definition. This can result in the followingparadoxical behaviour:
>>>importimp>>>importcls>>>c=cls.C()# Create an instance of C>>>imp.reload(cls)<module 'cls' from 'cls.py'>>>>isinstance(c,cls.C)# isinstance is false?!?False
The nature of the problem is made clear if you print out the “identity” of theclass objects:
>>>hex(id(c.__class__))'0x7352a0'>>>hex(id(cls.C))'0x4198d0'
Enter search terms or a module, class or function name.