Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How do I pass a variable by reference?

I wrote this class for testing:

class PassByReference:
 def __init__(self):
 self.variable = 'Original'
 self.change(self.variable)
 print(self.variable)
 def change(self, var):
 var = 'Changed'

When I tried creating an instance, the output was Original. So it seems like parameters in Python are passed by value. Is that correct? How can I modify the code to get the effect of pass-by-reference, so that the output is Changed?


Sometimes people are surprised that code like x = 1, where x is a parameter name, doesn't impact on the caller's argument, but code like x[0] = 1 does. This happens because item assignment and slice assignment are ways to mutate an existing object, rather than reassign a variable, despite the = syntax. See Why can a function modify some arguments as perceived by the caller, but not others? for details.

See also What's the difference between passing by reference vs. passing by value? for important, language-agnostic terminology discussion.

Answer*

Draft saved
Draft discarded
Cancel
19
  • 78
    -1. The code shown is good, the explanation as to how is completely wrong. See the answers by DavidCournapeau or DarenThomas for correct explanations as to why. Commented Jan 7, 2012 at 6:41
  • 5
    "...parameter passed in..." is incorrect use of terminology. A parameter is a named entity in a function (or method) definition that specifies an argument (or in some cases, arguments) that the function can accept. An argument is a value passed to a function (or method) when calling the function. Commented Feb 10, 2014 at 23:47
  • 3
    @EthanFurman You are wrong. Even if you read the link in DavidCournapeau's answer that is Call By Object, you will see that all of the expressions "call by object", "call by sharing" or "call by object reference", have the same meaning that is using an address to access the object. And for sure the address has to be determined from namespace. I can't accept that "Call by object reference" is different from "Call by reference". Commented Jul 9, 2018 at 9:21
  • 3
    @ML_Pro, "pass by assignment" seems to be a term made up by the Python documenters to describe "pass by value". For the user, I see no functional difference between passing a value that is a reference (or handle) as happens in languages such as Java or C# and what Python does, and I'd never use the term "pass by assignment"; it was edited into the answer, I assume to align with the documentation. Commented Aug 4, 2018 at 9:57
  • 3
    @HappyAhmad it absolutely is different to call by reference. If Python supported call by reference, you could do something like def foo(&var): var = 2 then x = 0; y = 1; foo(x); foo(y) then print(x, y) would print 2 2 Commented Mar 13, 2021 at 19:58

lang-py

AltStyle によって変換されたページ (->オリジナル) /