= (i+5 for i in range(10)) # (5, 6, ..., 14)
really a generator expression?Comprehensive Python Cheatsheet (2018)
51–60 of 83 posts
Re: Comprehensive Python Cheatsheet (2018)
#52Earlier quoted context omitted.
Is there some way to list all modules available for importing? Some times you don't know which modules are available to ask the help for it.
>>> help('modules')
Get names and types of a Python module's attributes:
https://jugad2.blogspot.com/2016/10/get-names-and-types-of-p...
and the same recipe on ActiveState Code (from where you can download the Python code for the recipe):
https://code.activestate.com/recipes/580705-get-names-and-ty...
Re: Comprehensive Python Cheatsheet (2018)
#53Earlier quoted context omitted.
As of 3.7 (3.6 for cpython), dictionaries have a deterministic order.
What's the best way to write code that relies on this deterministic order and prevent it from running incorrectly with older python versions?
(Dicts in 3.7 and ordered dicts)
https://stackoverflow.com/questions/50872498/will-ordereddic...
Re: Comprehensive Python Cheatsheet (2018)
#54Also, for me the main link won't actually display and I can't be bothered to track down why, but the Github source (https://github.com/gto76/python-cheatsheet) displays just fine.
Re: Comprehensive Python Cheatsheet (2018)
#55 (self, key, default) => self[key] = (key in self ? self[key] : default)
I'd prefer something like "dict#createifnotexists", but better.Re: Comprehensive Python Cheatsheet (2018)
#56 no_duplicates = list(dict.fromkeys())Re: Comprehensive Python Cheatsheet (2018)
#57Empty set literal: {*()}
Re: Comprehensive Python Cheatsheet (2018)
#58Earlier quoted context omitted.
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…
More importantly, in Python 3.7 this exception has become the rule, i.e. it was introduced in the language specification
Re: Comprehensive Python Cheatsheet (2018)
#59Is 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.
Re: Comprehensive Python Cheatsheet (2018)
#60Nice, but why does this needs JS to be rendered ?
Because it's rendered directly from README.md. That way it's easier for me, because I don't have to render it every time I make a change (I make a lot of little edits all the time), and project's Github page ( https://github.com/gto76/python-cheatsheet ) always has the same content as webpage.