classification
Title: Assignment Operators behavior within a user-defined function and arguments being passed by reference or value
Type: behavior Stage:
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: alhabshi3k, mark.dickinson, r.david.murray, steven.daprano
Priority: normal Keywords:

Created on 2014-09-28 15:26 by alhabshi3k, last changed 2014-09-28 16:23 by r.david.murray. This issue is now closed.

Messages (4)
msg227761 - (view) Author: Mohammed Mustafa Al-Habshi (alhabshi3k) Date: 2014-09-28 15:26
hello every one,

I was trying to understand the behavior of passing arguments in a function to differentiate how we can pass argument by value. Though it is a technique matter. However, the behavior of assignment operator += 
when using it with a list is more to toward behavior to the "append" method of the list object, and not like a a normal assignment. 

This causes a confusing when teach python language concepts , especially the behavior of += with numerical data types is list normal assignment and the parameters are then passed by value.

The issue is more related to data type mutability. and I believe assignment operator should be synthetically more compatible with normal assignment.


---- inline code example -----
def pass_(x):
    # x is list type
    print "Within the function"
    print " x was " , x
    #x = x + [50] #  here x is passed by value
    x += [50]    #  here x is passed by reference.
    #x.append(50)   #  here x is passed by reference.
    print " x then is " , x
    return

x = [12,32,12]
pass_(x)
print "\n x out of the function is " , x
msg227762 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2014-09-28 16:04
I'm afraid this bug tracker isn't really the appropriate place for this discussion, so I'm going to close this issue.  You could open a discussion on the Python mailing list, here: https://mail.python.org/mailman/listinfo/python-list.
msg227763 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2014-09-28 16:11
I'm afraid that you are mistaken about Python's argument passing semantics. Arguments are *always* passed using the same semantics, and *never* using either pass-by-value or pass-by-reference.

These two pages may help you understand why Python's argument passing semantics are always the same:

http://import-that.dreamwidth.org/1130.html
http://effbot.org/zone/call-by-object.htm

(Unfortunately, although this is the standard argument passing semantics used in many modern languages, including Python, Java and Ruby, there is no standard name for it.)

Augmented assignment is sometimes a little tricky to understand, because it may use both in-place mutation and assignment at the same time. But arguments are still always passed the same way.

If you have a concrete suggestion for a documentation change that will help reduce this confusion, please tell us. Otherwise, I think this issue can be closed. This is not the right place to discuss Python's argument passing semantics, but if you would like to discuss it, I'm happy to do so in the comments on the first link, or on the python-list@python.org mailing list.
msg227764 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-28 16:23
See also issue 20135.
History
Date User Action Args
2014-09-28 16:23:48r.david.murraysetnosy: + r.david.murray
messages: + msg227764
2014-09-28 16:11:50steven.dapranosetnosy: + steven.daprano
messages: + msg227763
2014-09-28 16:04:28mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg227762

resolution: not a bug
2014-09-28 15:26:49alhabshi3kcreate