[HN Gopher] The Best* Python Cheat Sheet
___________________________________________________________________
The Best* Python Cheat Sheet
Author : kmh
Score : 34 points
Date : 2024-04-01 03:01 UTC (20 hours ago)
(HTM) web link (kieranholland.com)
(TXT) w3m dump (kieranholland.com)
| kmh wrote:
| A 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
|
| *It may not be the best Python cheat sheet, but it aspires to be.
| kmh wrote:
| Cheat sheet link: https://kieranholland.com/best-python-cheat-
| sheet/
| beardyw wrote:
| I have recently started using python. This is exactly what I need
| / needed! Thank you.
| nick238 wrote:
| On 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 wrote:
| This 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 wrote:
| A 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 wrote:
| Decorators don't have to return functions. It's just nice to.
| The result of the call is bound to the name
| ptx wrote:
| It 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.
| Spivak wrote:
| Yeah the standard "I'm gonna make a decorator for this"
| template is def decorate(<decorator args>):
| def _decorate(<thing to be decorated>): pass
| return _decorate @decorate(foo=1) def
| myfunc(): pass
___________________________________________________________________
(page generated 2024-04-01 23:02 UTC)