Live data from Hacker News

Comprehensive Python Cheatsheet (2018)

gto76.github.io

61–70 of 83 posts

Re: Comprehensive Python Cheatsheet (2018)

#61

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.

Or get_or_create, to follow Django's example.

Re: Comprehensive Python Cheatsheet (2018)

#62
post #51

In the section "Inline -> Comprehension", isn't this: = (i+5 for i in range(10)) # (5, 6, ..., 14) really a generator expression?

Yes, although there is a saying that "every generator is an iterator, but not every iterator is a generator". But then again generator has a bunch of methods (close, gi_frame, gi_yieldfrom, throw, gi_code, gi_running, send) that iterators don't have... I really don't know if is correct enough here, or should I use (that I don't use anywhere else and could be confusing).

Re: Comprehensive Python Cheatsheet (2018)

#64
post #48

Earlier quoted context omitted.

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 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 ``.

Re: Comprehensive Python Cheatsheet (2018)

#65
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 ` `.

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)

#66
post #51

In the section "Inline -> Comprehension", isn't this: = (i+5 for i in range(10)) # (5, 6, ..., 14) really a generator expression?

Yes, although there is a saying that "every generator is an iterator, but not every iterator is a generator". But then again generator has a bunch of methods (close, gi_frame, gi_yieldfrom, throw, gi_code, gi_running, send) that iterators don't have... I really don't know if is correct enough here, or should I use (that I don't use anywhere else and could be confusing).

I would've written:

= ( f(i) for i in )

where f() is some function of i.

Re: Comprehensive Python Cheatsheet (2018)

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

Re: Comprehensive Python Cheatsheet (2018)

#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.print_exc()
            pdb.post_mortem()
        sys.exit(ret)
This means that whenever an uncaught exception gets raised, I immediately get told what happened (full backtrace) plus I get dumped into the debugger, from where I can inspect why this happened. I liberally sprinkle assert()s through my code, so this gives me a good edit-run-debug cycle to work with.
Post reply on HN