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

random.Random implements __getstate__() and __reduce__() #61691

Closed
vterron mannequin opened this issue Mar 20, 2013 · 5 comments
Closed

random.Random implements __getstate__() and __reduce__() #61691

vterron mannequin opened this issue Mar 20, 2013 · 5 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@vterron
Copy link
Mannequin

vterron mannequin commented Mar 20, 2013

BPO 17489
Nosy @rhettinger, @mdickinson
Files
  • issue17489_example.py
  • 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 2013-03-23.13:49:35.561>
    created_at = <Date 2013-03-20.01:49:12.667>
    labels = ['type-feature', 'library']
    title = 'random.Random implements __getstate__() and __reduce__()'
    updated_at = <Date 2013-04-02.17:13:29.904>
    user = 'https://bugs.python.org/vterron'

    bugs.python.org fields:

    activity = <Date 2013-04-02.17:13:29.904>
    actor = 'python-dev'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2013-03-23.13:49:35.561>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2013-03-20.01:49:12.667>
    creator = 'vterron'
    dependencies = []
    files = ['29513']
    hgrepos = []
    issue_num = 17489
    keywords = []
    message_count = 5.0
    messages = ['184721', '184746', '184800', '184867', '185850']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'mark.dickinson', 'python-dev', 'Ramchandra Apte', 'vterron']
    pr_nums = []
    priority = 'low'
    resolution = 'wont fix'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue17489'
    versions = ['Python 3.4']

    @vterron
    Copy link
    Mannequin Author

    vterron mannequin commented Mar 20, 2013

    The random.Random class implements both the __getstate__() and __reduce__() methods. My knowledge of the pickle module is very limited, but apparently this causes __getstate__() never to get called. Maybe this is a remnant from earlier code, or are both methods actually needed?

    @vterron vterron mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Mar 20, 2013
    @RamchandraApte
    Copy link
    Mannequin

    RamchandraApte mannequin commented Mar 20, 2013

    __getstate__ is for pickling instances [0], __reduce__ is for pickling the object itself [1]

    ^0 http://docs.python.org/2/library/pickle.html#object.\_\_getstate__
    ^1 http://docs.python.org/2/library/pickle.html#object.\_\_reduce__

    @vterron
    Copy link
    Mannequin Author

    vterron mannequin commented Mar 20, 2013

    Ramchandra, that is not the behavior that I am able to reproduce. Please see the attached example: in the presence of __reduce__(), __getstate__() is not being called whether the class or an instance its pickled. This is also in consonance with what is stated in the docs, which warn that "Although powerful, implementing __reduce__() directly in your classes is error prone. For this reason, class designers should use the high-level interface (i.e., __getnewargs__(), __getstate__() and __setstate__()) whenever possible".

    @rhettinger rhettinger self-assigned this Mar 21, 2013
    @rhettinger
    Copy link
    Contributor

    Possibly __getstate__ could be removed. That said, I would feel a sense of risk in taking it out -- that method has been present for a *very* long time who knows what might rely on it being there.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 2, 2013

    New changeset 68ed5b2ca867 by R David Murray in branch 'default':
    bpo-17489: Add explanatory comment that __getstate__ is not called.
    http://hg.python.org/cpython/rev/68ed5b2ca867

    @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
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant