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

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

File name Uploaded Description Edit, 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.

Good luck!
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