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

__init__ function may incur an incorrect behavior if passing a list as a parameter and set its default value as empty #87049

Closed
haoyang9804 mannequin opened this issue Jan 10, 2021 · 3 comments
Labels
3.9 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@haoyang9804
Copy link
Mannequin

haoyang9804 mannequin commented Jan 10, 2021

BPO 42883
Nosy @serhiy-storchaka, @haoyang9804
Files
  • test.py: This code snippet trigger this incorrect behavior.
  • 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 2021-01-10.14:58:42.480>
    created_at = <Date 2021-01-10.14:43:44.558>
    labels = ['type-bug', 'invalid', '3.9']
    title = '__init__  function may incur an incorrect behavior if passing a list as a parameter and set its default value as empty'
    updated_at = <Date 2021-01-10.15:00:28.855>
    user = 'https://github.com/haoyang9804'

    bugs.python.org fields:

    activity = <Date 2021-01-10.15:00:28.855>
    actor = 'haoyang9804'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-01-10.14:58:42.480>
    closer = 'serhiy.storchaka'
    components = []
    creation = <Date 2021-01-10.14:43:44.558>
    creator = 'haoyang9804'
    dependencies = []
    files = ['49732']
    hgrepos = []
    issue_num = 42883
    keywords = []
    message_count = 3.0
    messages = ['384765', '384766', '384767']
    nosy_count = 2.0
    nosy_names = ['serhiy.storchaka', 'haoyang9804']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue42883'
    versions = ['Python 3.9']

    @haoyang9804
    Copy link
    Mannequin Author

    haoyang9804 mannequin commented Jan 10, 2021

    Here is the bug-triggered code snippet in the file uploaded

    class A:
        def __init__(self, b=[]):
            print('b = ', b)
            self.a = b 
    
    for i in range(3):
        a = A()
        a.a.append(1)
    
        print(a.a)

    It seems that when I pass a list "b" to __init__ function with default value empty list. Every time I create a new instance of this class and append one new variable to "self.a", the default value of "b" changed at the next time I create another instance of class A.

    The outcome of this code snippet is
    a = []
    [1]
    a = [1]
    [1, 1]
    a = [1, 1]
    [1, 1, 1]

    I am new to python. Is it a legal behavior in python? If yes, what is the principle beneath it? Thanks in advance!

    @haoyang9804 haoyang9804 mannequin added 3.9 only security fixes type-bug An unexpected behavior, bug, or error labels Jan 10, 2021
    @haoyang9804
    Copy link
    Mannequin Author

    haoyang9804 mannequin commented Jan 10, 2021

    Thanks!

    @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
    3.9 only security fixes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant