classification
Title: Assignment operation of list is not working as expected
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: paul.moore, steve.dower, steven.daprano, tim.golden, yashwanthbarad@gmail.com, zach.ware
Priority: normal Keywords:

Created on 2020-07-15 07:59 by yashwanthbarad@gmail.com, last changed 2020-07-15 08:28 by steven.daprano. This issue is now closed.

Files
File name Uploaded Description Edit
List_assignment_in_functions.py yashwanthbarad@gmail.com, 2020-07-15 07:59 File with a program 1.with normal assignment, 2. Shorthand assignment having list assignment
Messages (1)
msg373672 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-07-15 08:28
Sorry Yashwanthbarad, this isn't a bug, you expect the wrong result.

Augmented assignment for lists is documented to be the same as calling the extend method, which means your AppenedFuncShortHandOperator function modifies the argument list in-place.

The AppenedFuncRegularAssignment simply replaces the *local variable* inside the function with a new list. Since it doesn't return the new list, the new list gets thrown away when the function returns, leaving the original list untouched.

This is not a bug, it is the normal behaviour. If you need help understanding why it works the way it does, and the difference between mutating an object and binding a new object to a local variable, there are many places you can ask for help, such as Stackoverflow, the /learnpython sub-reddit on Reddit, the tutor mailing list and others.

https://mail.python.org/mailman/listinfo/tutor

Good luck!
History
Date User Action Args
2020-07-15 08:28:02steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg373672

resolution: not a bug
stage: resolved
2020-07-15 07:59:49yashwanthbarad@gmail.comcreate