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.
Comprehensive Python Cheatsheet (2018)
61–70 of 83 posts
Re: Comprehensive Python Cheatsheet (2018)
#62In the section "Inline -> Comprehension", isn't this: = (i+5 for i in range(10)) # (5, 6, ..., 14) really a generator expression?
Re: Comprehensive Python Cheatsheet (2018)
#63So 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( ))
no_duplicates = set()Re: Comprehensive Python Cheatsheet (2018)
#64Earlier 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…
Re: Comprehensive Python Cheatsheet (2018)
#65Earlier 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 ` `.
Re: Comprehensive Python Cheatsheet (2018)
#66In 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).
= ( f(i) for i in )
where f() is some function of i.
Re: Comprehensive Python Cheatsheet (2018)
#67Is 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)
#68However, 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.Re: Comprehensive Python Cheatsheet (2018)
#69For me this is the best quick reference for Python (2.7 unfortunately): http://rgruet.free.fr/PQR27/PQR2.7.html
Re: Comprehensive Python Cheatsheet (2018)
#70Are there any available for Ruby or Linux?