- Include this in the python header or template file so that it is automatically included everytime a .py file is created.
- Write print functions as
- Format print functions with
- Import custom modules as
from . import mymodule
Dealing with exceptions
- Raising exceptions is
raise "Your useful error message"in python2 but should be written as
raise Exception("Your useful error message")
Text versus binary
- Handle binary and text with edge code (reading in and writing out) that decodes and encodes binary data.
strin python2 was a valid type for both text and binary data, but python3 makes the distinction clear with
strfor text and
- Do not use
unicodetype, instead just use
unicodein python2 was a valid type for unicode but does not exist in python3.
- In python2 we could examine the value of the dictionary with
dict[dict.keys()]. However python3
dict.keys()returns a non-indexable iterable.
- Get arbitary key of dictionary with
- Get arbitary item of dictionary with
Indexing at 0 is arbitary because python dictionaries are unordered.
cPicklein python2 is now
importlibin python2 is now
- Use generator comprehensions instead of list comprehensions if performing operations on the sequence.
sum(x for x in xrange(100000))
- This is both more time ans space efficient especially on huge lists. Both list and generator are compatible in both python2 and 3.
90% of this is written from own experience. For a better understanding on testing, packaging code and other software-engineering oriented practices, best to consult the official docs.