\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Kalamar} \pdfinfo{ /Title (python-3-core.pdf) /Creator (Cheatography) /Author (Kalamar) /Subject (Python 3 Core Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Python 3 Core Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Kalamar} via \textcolor{DarkBackground}{\uline{cheatography.com/53430/cs/14422/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Kalamar \\ \uline{cheatography.com/kalamar} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 14th February, 2018.\\ Updated 14th February, 2018.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Virtual Environment setup}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`sudo pip install virtualenv \# sudo may by optional`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`mkdir testenv`} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`cd testenv`} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`virtualenv -p \$( which python3.4 ) .lpvenv`} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`source .lpvenv/bin/activate`} \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{This set of commands is for a unix-like environment, but a similar setup can be done on Windows.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Core types}} \tn % Row 0 \SetRowColor{LightBackground} Integers: unlimited & cast: `int(value)` \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Boolean: `True`/`False` & cast: `bool(value)` \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} & any non-zero is `True` \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Real: 64 bits} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} Decimal: arbitrary precision & `from decimal import Decimal` \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} Complex: `1 + 2j` & methods: `real`, `imag` \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Strings}} \tn % Row 0 \SetRowColor{LightBackground} `""` & empty string \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `len(value)` & built-in to get the length \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}`s{[}start:stop:step{]}` & slicing, start/stop/step are optional, stop is non-inclusive \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{`s.encode('utf-8')`} & utf-8 encoded version of s, bytes object \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `b"string"` & bytes object \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.48 cm} x{5.52 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Tuples}} \tn % Row 0 \SetRowColor{LightBackground} `()` & empty tuple \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `(val,)` & 1-element tuple, comma is required \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} `(a,b,c)` & generic syntax for tuples \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} `a,b,c = 1,2,3` & implicit on assignment \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Tuples are immutable} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.344 cm} x{3.496 cm} p{0.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Lists}} \tn % Row 0 \SetRowColor{LightBackground} `{[}{]}` or list() & empty list & \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `list((create, from, a, tuple))` & \_ & \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} `{[}x + 5 for x in (1,2,3){]}` & definition by comprehension & \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} + & concatenate lists & \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} * & repeat list & \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{\{\{nobreak\}\}Notable methods: `append(x), count(x), extend(list), index(x), insert(pos,value), pop(), pop(pos), remove(val), reverse(), sort(), clear()` \newline Notable functions: `min(list), max(list), sum(list), len(list)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.2 cm} x{4.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dictionaries}} \tn % Row 0 \SetRowColor{LightBackground} `dict()` or `\{\}` & empty dictionary \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Equivalent definitions & `dict(A=1, Z=-1)` \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} & `\{'A': 1, 'Z': -1\}` \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} & `dict(zip({[}'A', 'Z'{]}, {[}1, -1{]})) ` \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} & `dict({[}('A', 1), ('Z', -1){]}) ` \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} & `dict(\{'Z': -1, 'A': 1\})` \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{nobreak\}\}Notable operations: `len, del, in` \newline Notable methods: `clear(), keys(), values(), items(), popitem(), pop(key), update(\{key, value\}), update(key=value), get(key), setdefault`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Sets}} \tn % Row 0 \SetRowColor{LightBackground} `set()` & empty set \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `value in set` & test for presence \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Mutable, for immutable, use frozenset. \newline Notable methods: `add(x), remove(x)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Conditions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{if condition: \newline stuff \newline else: \newline stuff \newline \newline if condition: \newline stuff \newline elif condition2: \newline stuff \newline elif condition3: \newline stuff \newline else: \newline stuff \newline \newline ternary = a if condition else b} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Iteration}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{for i in range(start,stop,step): \newline stuff \newline \newline for value in {[}sequence{]}: \newline stuff \newline \newline for position, value in enumerate(sequence): \newline stuff \newline \newline for a,b in zip(first, second): \newline stuff \newline \newline for \#\#\# \newline else \newline stuff to do at end of loop (usually exception when breaking in loop) \newline \newline while condition: \newline stuff \newline else \newline stuff to do when condition is false \newline \newline break \# breaks the loop \newline continue \# continue at the start of the loop body} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{module itertools provides lots of interesting tools for iteration} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Exceptions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{try: \newline \# do something \newline except ExceptionName as e: \newline \# do something \newline except (ExceptionName, OtherException) as e: \newline \# do something \newline else: \newline \# do something when no exception \newline finally: \newline \# do something anyway, exception or not} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.84 cm} x{4.16 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`def function\_name(args):\{\{nl\}\}~~body`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `nonlocal variable\_name` & access to nearest enclosing scope for variable \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} `global variable\_name` & access global scope variable \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `def func(a,b,c):` & standard positional args \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `func(c=1, b=2, a=3)` & call using keywords \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} `def func(a,b=55,c=85:)` & default values for missing args, !! be wary of mutable defaults \tn % Row Count 14 (+ 4) % Row 6 \SetRowColor{LightBackground} `def func(*n):` & variable arg list, addressed as an array \tn % Row Count 16 (+ 2) % Row 7 \SetRowColor{white} `func(*n)` & calls func unpacking array argument \tn % Row Count 18 (+ 2) % Row 8 \SetRowColor{LightBackground} `def func(**kwargs):` & variable keyword arguments, addressed as a dict \tn % Row Count 21 (+ 3) % Row 9 \SetRowColor{white} `func(**n)` & calls func unpacking dict argument \tn % Row Count 23 (+ 2) % Row 10 \SetRowColor{LightBackground} `pass` & empty body method \tn % Row Count 24 (+ 1) % Row 11 \SetRowColor{white} `return a,b,c` & return multiple values \tn % Row Count 26 (+ 2) % Row 12 \SetRowColor{LightBackground} `lambda {[}parameter\_list{]}: expression` & for small snippets \tn % Row Count 28 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`def func(n):\{\{nl\}\}~~"""documentation, possibly multiline"""\{\{nl\}\}~~pass`} \tn % Row Count 30 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{nobreak\}\}Special attributes: ` \_\_doc\_\_, \_\_name\_\_, \_\_qualname\_\_, \_\_module\_\_, \_\_defaults\_\_, \_\_code\_\_, \_\_globals\_\_, \_\_dict\_\_, \_\_closure\_\_, \_\_annotations\_\_, \_\_kwdefaults\_\_` \newline builtins: `abs all any ascii bin callable chr compile delattr dir divmod eval exec format getattr globals hasattr hash hex id input isinstance issubclass iter len locals max min next oct open ord pow print repr round setattr sorted sum vars`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Utilities}} \tn % Row 0 \SetRowColor{LightBackground} `map(function, iterable, optional\_iterable, ...)` & returns iterator which will apply function on each value of the iterables, stopping at first exhausted iterable \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} `zip(*iterables)` & returns iterator of tuples, interlacing the iterables, stops at first exhausted iterable \tn % Row Count 11 (+ 5) % Row 2 \SetRowColor{LightBackground} `filter(function, iterable)` & returns iterator from those elements of iterable for which function returns True \tn % Row Count 15 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.84 cm} x{6.16 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Comprehensions}} \tn % Row 0 \SetRowColor{LightBackground} list & `{[} expression for iterable\_clause if optional\_filter{]}` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} nested lists & `{[} expression for iterable\_clause\_1 for iterable\_clause\_2 if optional\_filter{]}` \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} dict & ` \{ key\_expression: value\_expression for iterable\_clause if optional\_filter\}` \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} set & `\{ value\_expression for iterable\_clause if optional\_filter\}` \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Generator functions}} \tn % Row 0 \SetRowColor{LightBackground} `yield x` & returns the value x, suspends and stores the internal state \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `next(function)` & resumes execution (or start the first time) \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{until loop ends with no value or StopIteration is raised} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{`generator.send(x)`} & resumes and makes x as the return value of yield \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{usage example: `for in in \seqsplit{generator\_function(**some\_params)`}} \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} `yield from \seqsplit{list\_comprehension`} & advanced pattern \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Generator expression}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{like a list comprehension but ( ) instead of {[}{]}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{returns 1 item at a time} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{easier to read than map+filter} \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Decorators}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def wrap(func): \newline def wrapper(*args, **kwargs): \newline \# do something about func \newline func(*args, **kwargs) \newline \# do something about func \newline return wrapper \newline \newline \# Apply decorator \newline def to\_decorate(...): \newline \# body \newline to\_decorate = wrap(to\_decorate) \newline \newline \# More idiomatic \newline @wrap \newline def to\_decorate(...): \newline \#body \newline \newline \newline from functools import wraps \newline @wraps(func) \newline def wrapper(...) \# to keep the name and doc from the wrapped function \newline \newline \# Decorator with args: make a decorator factory \newline def \seqsplit{decorator\_factory(factory\_args):} \newline def decorator(func): \newline def wrapper(*args, **kwargs): \newline \# do something about func \newline func(*args, **kwargs) \newline \# do something about func \newline return wrapper \newline return decorator \newline \newline \newline @decorator\_factory(1,2...) \newline def to\_decorate(...):} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Multiple decorators: the closer the decorator to the function, the sooner it is applied.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Classes}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}`class~Child(Parent):` & is-a inheritance \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `class Child(Multiple, Parent):` & `Child.mro()` returns Method Resolution Order \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} `def \_\_init(self, args):` & initializer \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{`super().\_\_init\_\_(self}, some\_args)` & call parent initializer \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} `def \seqsplit{instance\_method(self}, args):` & `self` is a convention for the first argument, the implicit instance \tn % Row Count 13 (+ 4) % Row 5 \SetRowColor{white} `instance = ClassName(params)` & create instance \tn % Row Count 15 (+ 2) % Row 6 \SetRowColor{LightBackground} `@staticmethod` & annotation for static method, no special argument (like `self`) \tn % Row Count 19 (+ 4) % Row 7 \SetRowColor{white} \{\{nobreak\}\}`@classmethod` & annotation for static method, special argument is the class object itself (convention: `cls`). Usage: factory methods or extracting sub-methods from @staticmethods methods (to avoid having a hard-coded class name when calling them) \tn % Row Count 31 (+ 12) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Classes (cont)}} \tn % Row 8 \SetRowColor{LightBackground} `\_attribute` & pseudo private attribute (convention) \tn % Row Count 2 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{`\_\_some\_attr\_or\_method`} & almost private through name mangling \tn % Row Count 4 (+ 2) % Row 10 \SetRowColor{LightBackground} `@property` & for getter \tn % Row Count 5 (+ 1) % Row 11 \SetRowColor{white} `@property.setter` & for setter \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{a class is a subclass of itself; attributes can be added in declaration (class attributes) or dynamically to instance or class} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Custom Iteration}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{iterable must define `\_\_iter\_\_` or `\_\_get\_item\_\_`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{iterator must define `\_\_iter\_\_` (returning self) and `\_\_next\_\_` (raise `StopIteration` at the end)} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{usage: `for i in iterable:`} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}