Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operator.setitem example no longer works in Python 3 due to lazy map #66376

Closed
mwilliamson mannequin opened this issue Aug 10, 2014 · 6 comments
Closed

operator.setitem example no longer works in Python 3 due to lazy map #66376

mwilliamson mannequin opened this issue Aug 10, 2014 · 6 comments
Assignees
Labels
docs Documentation in the Doc dir

Comments

@mwilliamson
Copy link
Mannequin

mwilliamson mannequin commented Aug 10, 2014

BPO 22180
Nosy @rhettinger, @pitrou, @bitdancer
Files
  • doc-operator-example.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/rhettinger'
    closed_at = <Date 2014-08-10.17:35:07.156>
    created_at = <Date 2014-08-10.14:18:24.747>
    labels = ['docs']
    title = 'operator.setitem example no longer works in Python 3 due to lazy map'
    updated_at = <Date 2014-08-10.17:35:07.155>
    user = 'https://bugs.python.org/mwilliamson'

    bugs.python.org fields:

    activity = <Date 2014-08-10.17:35:07.155>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2014-08-10.17:35:07.156>
    closer = 'rhettinger'
    components = ['Documentation']
    creation = <Date 2014-08-10.14:18:24.747>
    creator = 'mwilliamson'
    dependencies = []
    files = ['36333']
    hgrepos = []
    issue_num = 22180
    keywords = ['patch']
    message_count = 6.0
    messages = ['225141', '225142', '225145', '225146', '225147', '225149']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'pitrou', 'r.david.murray', 'docs@python', 'python-dev', 'mwilliamson']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue22180'
    versions = ['Python 3.4', 'Python 3.5']

    @mwilliamson
    Copy link
    Mannequin Author

    mwilliamson mannequin commented Aug 10, 2014

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

    @mwilliamson mwilliamson mannequin assigned docspython Aug 10, 2014
    @mwilliamson mwilliamson mannequin added the docs Documentation in the Doc dir label Aug 10, 2014
    @bitdancer
    Copy link
    Member

    Heh. There was a discussion in bpo-22106 about valid examples for using 'pass'. This case is analogous to the one I came up with.

      for x in map(...):
         pass

    that avoids building a list.

    Not that any of it is idiomatic, as you say.

    @pitrou
    Copy link
    Member

    pitrou commented Aug 10, 2014

    The whole example is bad and should be removed or replace with something else:

    1. Using map() is an anti-pattern here, since the function results are not used.

    2. To "build a dictionary that maps the ordinals from 0 to 255 to their character equivalents", modern Python code should use a dict comprehension and avoid operator.setitem() entirely.

    @rhettinger
    Copy link
    Contributor

    The whole example is bad and should be removed
    or replace with something else:

    I'm not sure there are ANY examples of operator.setitem that couldn't be done a better way without it. How about we remove it and leave the examples for things that people ought to be doing.

    @rhettinger rhettinger assigned rhettinger and unassigned docspython Aug 10, 2014
    @pitrou
    Copy link
    Member

    pitrou commented Aug 10, 2014

    Le 10/08/2014 13:20, Raymond Hettinger a écrit :

    Raymond Hettinger added the comment:

    > The whole example is bad and should be removed
    > or replace with something else:

    I'm not sure there are ANY examples of operator.setitem that
    couldn't
    be done a better way without it. How about we remove it and leave the
    examples for things that people ought to be doing.

    Agreed. operator.setitem() isn't really ever used in my experience.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 10, 2014

    New changeset 9c250f34bfa3 by Raymond Hettinger in branch '3.4':
    Issue bpo-22180: Remove weak example
    http://hg.python.org/cpython/rev/9c250f34bfa3

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants