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

surprised by default list parameter #48348

Closed
trott mannequin opened this issue Oct 10, 2008 · 4 comments
Closed

surprised by default list parameter #48348

trott mannequin opened this issue Oct 10, 2008 · 4 comments
Labels
build The build process and cross-build type-bug An unexpected behavior, bug, or error

Comments

@trott
Copy link
Mannequin

trott mannequin commented Oct 10, 2008

BPO 4098
Nosy @amauryfa
Files
  • p6.py: just run the example file "p6.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 = None
    closed_at = <Date 2008-10-10.11:21:21.675>
    created_at = <Date 2008-10-10.09:23:43.974>
    labels = ['type-bug', 'invalid', 'build']
    title = 'surprised by default list parameter'
    updated_at = <Date 2008-10-10.11:21:21.634>
    user = 'https://bugs.python.org/trott'

    bugs.python.org fields:

    activity = <Date 2008-10-10.11:21:21.634>
    actor = 'amaury.forgeotdarc'
    assignee = 'none'
    closed = True
    closed_date = <Date 2008-10-10.11:21:21.675>
    closer = 'amaury.forgeotdarc'
    components = ['Build']
    creation = <Date 2008-10-10.09:23:43.974>
    creator = 'trott'
    dependencies = []
    files = ['11761']
    hgrepos = []
    issue_num = 4098
    keywords = []
    message_count = 4.0
    messages = ['74624', '74626', '74627', '74628']
    nosy_count = 3.0
    nosy_names = ['amaury.forgeotdarc', 'eckhardt', 'trott']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4098'
    versions = ['Python 2.6', 'Python 2.5', 'Python 2.5.3']

    @trott
    Copy link
    Mannequin Author

    trott mannequin commented Oct 10, 2008

    The attached file produced the following result:


    trott$ python2.5 p6.py
    []
    [1234]


    I expected both times the empty list since the print
    statement prints just a defaulted parameter ("e") which
    was defaulted to an empty list literal ("[]") and never set
    by supplying an actual class parameter.

    The object property "entries" appears to be shared by
    both instances. Why?

    @trott trott mannequin added build The build process and cross-build type-bug An unexpected behavior, bug, or error labels Oct 10, 2008
    @eckhardt
    Copy link
    Mannequin

    eckhardt mannequin commented Oct 10, 2008

    http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm

    The same recently cropped up on the users mailinglist under the topic
    "default value in __init__".

    @trott
    Copy link
    Mannequin Author

    trott mannequin commented Oct 10, 2008

    OK. I've read the Python Reference Manual 7.6 "Function Definition".
    It "explains" why this happens.

    BUT: Why can`t the compiler make a copy of the default object, if
    it's mutable (e.g. a list)? This can't be that difficult.

    @amauryfa
    Copy link
    Member

    Yes, the default value expression could be evaluated each time the
    function is called without the parameter. But it would be another language.
    Default values are computed once, and this is a Python feature.

    It can be useful, for example to cache computations, or to achieve
    "early binding" in nested functions, as explained in the suggested FAQ.

    @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
    build The build process and cross-build type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant