Live data from Hacker News

Comprehensive Python Cheatsheet (2018)

gto76.github.io

41–50 of 83 posts

Re: Comprehensive Python Cheatsheet (2018)

#41
post #31

Earlier quoted context omitted.

>>> list(dict.fromkeys([3, 2, 1])) [3, 2, 1] >>> list(set([3, 2, 1])) [1, 2, 3]

Dictionaries should not be assumed to keep their orderings anyway. That's why there's OrderredDict. Unless the language explicitly says that dicts hold their order, even if they do, it's an implementation detail, and it should not be relied upon.

If "the language explicitly says" something, then it's part of the specification and not an implementation detail.

Re: Comprehensive Python Cheatsheet (2018)

#42

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…

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)

#43
post #31

Earlier quoted context omitted.

Dictionaries should not be assumed to keep their orderings anyway. That's why there's OrderredDict. Unless the language explicitly says that dicts hold their order, even if they do, it's an implementation detail, and it should not be relied upon.

If "the language explicitly says" something, then it's part of the specification and not an implementation detail.

Sure, hence the "unless" I've used.

Re: Comprehensive Python Cheatsheet (2018)

#44

Earlier quoted context omitted.

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.

It's quite annoying to have to enable JS for 3 different domains just to see something other than a blank page. It also prevents non-JS browsers and retrievers from seeing content. If you were to use e.g. org-mode for the source document, you could easily export to HTML automatically when the file is saved. You could also easily use a git hook to run e.g. pandoc to convert md to HTML automatically. There are many way…

I would also encourage the author and others to export static documents to HTML. As a user, I really appreciate it.

Re: Comprehensive Python Cheatsheet (2018)

#46

Can we please change the current clickbait title (Best Python Cheatsheet Ever!) to the original one (Comprehensive Python Cheatsheet)?

Agreed, although I'll note here that this was posted to /r/python under the clickbait version of the title, so the HN submitter was likely just passing it on from there.

Re: Comprehensive Python Cheatsheet (2018)

#47
post #27

Earlier quoted context omitted.

I would put it after R help function and, even better, F1 key to get the full documentation of any thing in Rstudio.

I'm amazed you think the R documentation is anything but terrible. Compare the R vector page: https://stat.ethz.ch/R-manual/R-devel/library/base/html/vect... To the equivalent python documentation: https://docs.python.org/3/tutorial/datastructures.html

The documentation could clearly be prettier but its strength lies in its standardization : I can get method arguments and examples for all functions of all packages in the same format and with a single keystroke.

Re: Comprehensive Python Cheatsheet (2018)

#48
post #2

I like the clean look and the overall way it is presented. The examples using angle brackets (eg.: .append( ) ) is highly legible and the one column format is faster to scan.

I came here to say exactly the opposite. The angle brackets mean that every single example is a syntax error. If you are going to write a cheatsheet with samples of Python code, why not write it in valid Python? This is clearly aimed at beginners as the samples are or very basic things, yet this is going to be confusing to them as they will think that ` ` is syntax. Have a look at the Python docs for much better ways…

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 codes in statically typed languages and has an intuitive sense of upcasting / downcasting / contravariance / covariance, this level of precision makes sense to me and enhances my appreciation for Python types.

That said, I've also been writing Python professionally since 2005 so I'm comfortable with not worrying too much about types in Python -- with dynamic typing things just work as long as they have the right shape and behavior.

[1] https://docs.python.org/3/library/threading.html#threading.T...

Re: Comprehensive Python Cheatsheet (2018)

#50
post #31

Earlier quoted context omitted.

Dictionaries should not be assumed to keep their orderings anyway. That's why there's OrderredDict. Unless the language explicitly says that dicts hold their order, even if they do, it's an implementation detail, and it should not be relied upon.

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?
Post reply on HN