Skip to content

surprised by default list parameter #48348

Closed

Description

@trott
mannequin
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']

    Activity

    1. trott commented on Oct 10, 2008

      trottmannequin
      MannequinAuthor

      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?

    2. added
      buildThe build process and cross-build
      type-bugAn unexpected behavior, bug, or error
      on Oct 10, 2008
    3. eckhardt commented on Oct 10, 2008

      eckhardtmannequin
      Mannequin

      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__".

    4. trott commented on Oct 10, 2008

      trottmannequin
      MannequinAuthor

      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.

    5. amauryfa commented on Oct 10, 2008

      @amauryfa
      Contributor

      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.

    6. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      buildThe build process and cross-buildtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions