Live data from Hacker News

Comprehensive Python Cheatsheet (2018)

gto76.github.io

51–60 of 83 posts

Re: Comprehensive Python Cheatsheet (2018)

#52
post #32

Earlier 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')

Related post on my blog:

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)

#53

Earlier 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?

Import collections.OrderedDict as you would have done beforehand. There are still some differences between them.

(Dicts in 3.7 and ordered dicts)

https://stackoverflow.com/questions/50872498/will-ordereddic...

Re: Comprehensive Python Cheatsheet (2018)

#55
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.

Re: Comprehensive Python Cheatsheet (2018)

#56
So to get a list of unique values you suggest I convert to a dictionary and then convert back to a list? Is this the Python way? I dunno it seems like acrobatics

  no_duplicates = list(dict.fromkeys())

Re: Comprehensive Python Cheatsheet (2018)

#58

Earlier 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

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.

Re: Comprehensive Python Cheatsheet (2018)

#59

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.

[deleted]

Re: Comprehensive Python Cheatsheet (2018)

#60
post #7

Nice, 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.

Why not use static site generator and re-run on content update?
Post reply on HN