Live data from Hacker News

Comprehensive Python Cheatsheet (2018)

gto76.github.io

71–80 of 83 posts

Re: Comprehensive Python Cheatsheet (2018)

#71
post #48

Earlier quoted context omitted.

I think the author of this document is trying to differentiate between types (broad notions of course -- there's no type called 'num' in Python). The Python docs (example here[1]) on the other hand limit themselves to {}, [] and () to differentiate between dicts, lists and tuples, but doesn't really differentiate between these and the more generalized notions of collections/iterators/element, etc. As someone who code…

But he could achieve exactly the same thing by using descriptive variable names without the angle brackets. Admittedly he can't use `list` as it's a built-in but `a_list` or `list_` would be much better than ` `.

Good point. For list, one could still use the name "lst" and get the point across.

Re: Comprehensive Python Cheatsheet (2018)

#73

Earlier quoted context omitted.

Neither dict nor set preserve order. This is a misleading snippet.

In Python 3.6+, dictionaries preserve insertion order. This is done by storing the keys, values (and cached hash values) in a separate array, and the hashtable is a succinct array of indexes into this array. This results in more compact dictionaries, which are also a bit faster because of it's cache friendliness. Preserving insertion order is a happy side effect of this. This optimization can also be applied to Pytho…

Well I'll be dipped.

Re: Comprehensive Python Cheatsheet (2018)

#74

Earlier quoted context omitted.

More importantly, in Python 3.7 this exception has become the rule, i.e. it was introduced in the language specification

I am personally not a fan of details like this creeping into the language spec. CPython is not the only Python. This kind of spec creates unnecessary challenges for Micropython, for example. And of course there is Cython and others. It makes no sense to me that this should be in the language spec — you are essentially specifying a built in minefield of implementation bugs.

Cpython took this implementation detail from pypy (so it was already going to be the case in the two most used python implementations).

The reason being that the ordered dict ends up being faster than non-ordered, and people will rely on this implementation detail, so they added it to the spec to make that okay.

Re: Comprehensive Python Cheatsheet (2018)

#75
post #3

Very good page. But the best python cheat sheet is help(the_thing) . python has the second best ever help and discoverability (after Matlab of course!)

I could not help but comment to add Elixir to the list of languages with amazing discoverability. For things in the standard library, you essentially get a condensed version of Python web documentation, with examples!

Re: Comprehensive Python Cheatsheet (2018)

#76
post #68

I like the basic script template that the author provides. However, when I start development of new code I always use a script that starts off with traceback and pdb, something like this: #!/usr/bin/env python import traceback import pdb import sys def main(): # some WIP code that maybe raises an exception raise BaseException("oh no, exception!") return 0 if __name__ == "__main__": try: ret = main() except: traceback…

This is pretty smart. When I'm making one-off scripts I use the interactive flag

python -i script.py

I've programmed Python for years and never knew this. I'm not sure how well this handles errors. Tab complete works, only thing that is missing is needing to wrap help() to see signatures. I'm enjoying the standard lib interpreter.

Re: Comprehensive Python Cheatsheet (2018)

#77
post #67

Is it just me or is dict#setdefualt a terribly named function? I just learned of it here, and I was excited that it would set the default value for the dictionary, when instead it performs something like: (self, key, default) => self[key] = (key in self ? self[key] : default) I'd prefer something like "dict#createifnotexists", but better.

Are you maybe looking for a defaultdict? https://docs.python.org/3.7/library/collections.html#collect...

I'm aware of defaultdict, but when I saw that method on dict I got excited because I thought it meant I didn't need defaultdict anymore.

Re: Comprehensive Python Cheatsheet (2018)

#79

Earlier quoted context omitted.

But he could achieve exactly the same thing by using descriptive variable names without the angle brackets. Admittedly he can't use `list` as it's a built-in but `a_list` or `list_` would be much better than ` `.

My personal opinion is that `list_` is among the worst possible variable names (together with `bar` and `foo`). It's non-descriptive, ugly, hard to recognize, looks scary to beginners and maybe worst of all it's easy to forget the underscore when typing.

That's exactly what he's used, but with angle brackets around it. What do you think he should have done instead.

Re: Comprehensive Python Cheatsheet (2018)

#80
post #68

I like the basic script template that the author provides. However, when I start development of new code I always use a script that starts off with traceback and pdb, something like this: #!/usr/bin/env python import traceback import pdb import sys def main(): # some WIP code that maybe raises an exception raise BaseException("oh no, exception!") return 0 if __name__ == "__main__": try: ret = main() except: traceback…

This is pretty smart. When I'm making one-off scripts I use the interactive flag python -i script.py I've programmed Python for years and never knew this. I'm not sure how well this handles errors. Tab complete works, only thing that is missing is needing to wrap help( ) to see signatures. I'm enjoying the standard lib interpreter.

I use ipython for similar task. It has wrapper for 'help' function ('?' symbol after function name), tab completion and syntax highlighting. Also it has special %hist command for easy copy-paste code.
Post reply on HN