[issue22180] operator.setitem example no longer works in Python 3 due to lazy map
New submission from Michael Williamson: The Python docs for the operator module include an example using map and setitem to "Build a dictionary that maps the ordinals from 0 to 255 to their character equivalents.": d = {} keys = range(256) vals = map(chr, keys) map(operator.setitem, [d]*len(keys), keys, vals) Since map is lazy since Python 3, the dictionary d is never actually changed in this example. I'm not entirely sure what the idiomatic way to fix the example is since it strikes me as being fairly unidiomatic to begin with, but the simplest would be to call list on the result of map to force evaluation (patch attached). -- assignee: docs@python components: Documentation files: doc-operator-example.patch keywords: patch messages: 225141 nosy: docs@python, mwilliamson priority: normal severity: normal status: open title: operator.setitem example no longer works in Python 3 due to lazy map versions: Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5 Added file: http://bugs.python.org/file36333/doc-operator-example.patch ___ Python tracker <http://bugs.python.org/issue22180> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22243] Documentation on try statement incorrectly implies target of except clause can be any assignable expression
New submission from Michael Williamson: In the docs for the try statement [1], part of the grammar is: try1_stmt ::= "try" ":" suite ("except" [expression ["as" target]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] The `target` rule allows any assignable expression (identifier, attributes, etc.). However, CPython appears to only allow identifiers as a target. The abstract grammar in the ast module [2] appears to confirm this. [1] https://docs.python.org/3.4/reference/compound_stmts.html#the-try-statement [2] https://docs.python.org/3.4/library/ast.html#abstract-grammar -- assignee: docs@python components: Documentation messages: 225611 nosy: docs@python, mwilliamson priority: normal severity: normal status: open title: Documentation on try statement incorrectly implies target of except clause can be any assignable expression versions: Python 3.4, Python 3.5 ___ Python tracker <http://bugs.python.org/issue22243> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22243] Documentation on try statement incorrectly implies target of except clause can be any assignable expression
Michael Williamson added the comment: > Using 'expression' (or 'expr' in the ast doc) for what should be 'exceptions: > identifier' or paranthesized tuple of identifiers;, is also too broad. > However, that must be enforced in the compiler rather than the parser, and > compiler restrictions are in the text, not the grammar. I would expect any expression to be valid so long as it evaluates at runtime to an exception type (or tuple of exception types), so the use of expression seems correct to me. In other words, the following is valid Python (but would not be allowed if I've understood your statement correctly): def f(): try: "".blah except some_error(): print("caught") def some_error(): return AttributeError f() -- ___ Python tracker <http://bugs.python.org/issue22243> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com