Totally Confused: Passing variables to functions
W Isaac Carroll
icarroll at pobox.com
Thu Jun 5 04:20:58 EDT 2003
More information about the Python-list mailing list
Thu Jun 5 04:20:58 EDT 2003
- Previous message (by thread): Totally Confused: Passing variables to functions
- Next message (by thread): Totally Confused: Passing variables to functions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Chuck wrote: > I've hit a stumbling block while trying to pick up Python. I've googled > around, and I don't think I'm the first to have this question, but I haven't > been able to find an answer that explains things for me. [snip] > ... This gives me the impression that Python passes variables by reference (by > a "pointer"), and the "arg" in blah is the same as "v". [snip] > I get the impression Python passes variables by copy. > > *Huh?* > > It seems that if you pass a mutable variable, you can change it, but only by > using it's methods, ie arg.append(), and NOT by doing an "arg = (new value)". > > And if you pass an immutable variable, you can't change it at all. You are correct. > This doesn't seem very intuitive, which leads me to believe I'm missing "the > big picture". Right. It's not intuitive to a C programmer. See below for why. > In the python tutorial, it says: > > Actually, call by object reference would be a better description, > since if a mutable object is passed, the caller will see any changes > the callee makes to it (items inserted into a list). > > But I don't understand what "object reference" means. I'm familiar with "pass > by copy" and "by reference(pointer)" in the Pascal or C languages... "Call by object reference" is used because the "call by reference" vs "call by value" distinction tends to give people the wrong idea about Python. There was a long discussion of the subject recently that you should look at if you're interested in the arguments for and against that term. My understanding is that Python is technically "call by value", but that description leaves out the vital point that the only values in python are references. :) > What's going on? (*grin*) In order to understand argument passing in Python, you have to understand what variables and objects are in Python. In C, a variable is a named piece of memory that you can put stuff into. In Python, a variable is just a name. It doesn't have a type, and it only stores a reference to an object. An object in Python is a nameless piece of memory that you can put stuff into. It has a type and it may have attributes and methods (what would be called data and function members in C++). When you pass a variable to a function, you're actually passing a reference to the variable's object. If the function tells that object to change its value, the change is visible to the caller. If the function creates a new object, the value of the original object (and the caller's reference to it) will not be affected. I think that this could be described as the "big picture" of Python. It seems to be the most common stumbling block for new Pythoners coming from a C (and relateds) background. TTFN
- Previous message (by thread): Totally Confused: Passing variables to functions
- Next message (by thread): Totally Confused: Passing variables to functions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list