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 ` `.
Comprehensive Python Cheatsheet (2018)
71–80 of 83 posts
Re: Comprehensive Python Cheatsheet (2018)
#72This is quite useful as I don't always remember syntax. Are there any available for Ruby or Linux?
Re: Comprehensive Python Cheatsheet (2018)
#73Earlier 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…
Re: Comprehensive Python Cheatsheet (2018)
#74Earlier 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.
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)
#75Very 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!)
Re: Comprehensive Python Cheatsheet (2018)
#76I 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…
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)
#77Is 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...
Re: Comprehensive Python Cheatsheet (2018)
#78Re: Comprehensive Python Cheatsheet (2018)
#79Earlier 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.
Re: Comprehensive Python Cheatsheet (2018)
#80I 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.