4 ms·
The Best* Python Cheat Sheet
- kmh 3y agoA dense Python cheat sheet with just what you need. Design principles: • Focus on Python core • Comprehensive but selective (Just what you need) • Densely packed • Well-linked • Linkable • Responsive • Printable Issues and feedback are tracked at https://github.com/kieranholland/best-python-cheat-sheet https://github.com/kieranholland/best-python-cheat-sheet *It may not be the best Python cheat sheet, but it aspires to be.
- kmh 3y agoCheat sheet link: https://kieranholland.com/best-python-cheat-sheet/ https://kieranholland.com/best-python-cheat-sheet/
- kmh 2y agoThanks to everyone for your feedback. I've released a new version today which improves the order of sections, refines dark mode colors, fixes font on MacOS, details built-in functions, has more internal linking, includes set/dict comprehensions, has deeper Python doc links, and fixes errors. I agree decorator section and navigation could still be improved.
- youthsforum 3y ago[flagged]
- beardyw 3y agoI have recently started using python. This is exactly what I need / needed! Thank you.
- deleted 3y ago[deleted]
- nick238 3y agoOn macOS, Courier New is razor thin and hard to read. You could link the Google WebFont version of Roboto Mono by adding <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,100..700;1,100..700&display=swap" rel="stylesheet">
- mathfailure 3y agoThis cheat sheet is of quite poor quality: the very second thing it tries to explain is decorator: A decorator is a callable that manipulates and returns a function. # wraps decorator copies metadata of decorated function (func) to wrapped function (out) from functools import wraps def show_call(func): """ Print function name and arguments each time it is called. """ @wraps(func) def out(*args, **kwds): print(func.__name__, args, kwds) return func(*args, **kwds) return out @show_call def add(x, y): return x + y This example uses a decorator function show_call() when calling function add(), but that decorator function itself is not a simple function, but some weirdo that actually defines another function out() with *args and **kwargs THAT IS ALSO ALREADY DECORATED (with function wraps())! I couldn't have thought of a worse example for a cheat sheet. A cheat sheet is supposed to give short simple explanations/demonstrations. That example shouldn't have a nested function, it should demonstrate just 1 decorator function and 1 decorated function and none of these functions should be nested and they also shouldn't use arguments with asterisks (if possible). Feedback from someone still failing to understand what decorator is, especially after reading the cheat sheet.
- jonathrg 3y agoA decorator is a function that takes a function and returns a modified function - how would you write a decorator without nesting functions? Wrapping the inner function of the decorator with @wraps is quite common - one might consider it a bug in the language that the @wraps feature is not built into the decorator mechanism. But it is a valid point that decorators should not be presented that early.
- aunderscored 3y agoDecorators don't have to return functions. It's just nice to. The result of the call is bound to the name
- ptx 3y agoIt is confusing, but it's also kind of the standard pattern for decorators in Python, so it's a useful example. The decorator syntax itself is just simple syntax sugar for replacing the decorated function (add) with the return value of the decorator function (show_call). The example is essentially equivalent to this: def show_call(func): ... def add(x, y): return x + y add = show_call(add) So show_call has to return the function we want as a replacement, which is the nested "out" function here. The "@wraps" thing is not strictly necessary but updates the new function object to have the same name as the original.
- 2cynykyl 3y agoYou missed a great opportunity to hide a cheat right in the title! "The *Best Python Cheat Sheet"
- kmh 3y ago*thanks :)
- Biganon 3y agoTypo: __detitem__ should be __delitem__ Also, you described "s.rstrip" twice