1
class x:
 def __init__(self):
 self.y=None
 self.sillyFunc(self.y)
 def sillyFunc(self,argument):
 if argument is None:
 argument='my_name_as_argument'
 self.printy()
 def printy(self):
 print self.y

According to me the above code should print>my_name_as_argument,where am i going wrong?

asked Jun 20, 2010 at 14:16
1
  • Python uses call-by-sharing, which never changes what references point to. This makes mutable and immuntable objects appear to be treated differently, although they're really not. In your case, you're only assigning 'my_name_as_argument' to the local variable argument - you never do anything that would change the value of self.y. Commented Jun 20, 2010 at 18:06

3 Answers 3

5

The assigment

argument='my_name_as_argument'

only affects the local variable argument. It doesn't change what self.y points to.

answered Jun 20, 2010 at 14:23
4

In Python everything is an object and variables contain references to objects. When you make a function call it makes copies of the references. Some people including Guido van Rossum call this "Call by object reference". Important note from Wikipedia:

a function cannot change the value a variable references in its calling function.

The code as you posted it prints nothing at all. I think you mean to add this extra line to your program:

x()

This then results in the output: None. This is not surprising because you are printing the value of self.y but the only value you ever assign to self.y is None.

In Python, strings are immutable. Reassigning the value of argument only overwrites the local copy of the reference. It does not modify the original string.

As you asked in a comment, if you use a mutable object and you reassign the reference, again this doesn't do what you want - the original object is not affected. If you want to mutate a mutable object you can call a method that mutates it. Simply reassigning a reference does not change the original object.

If you want self.y to point to a new object then you have to assign the object reference directly to self.y.

answered Jun 20, 2010 at 14:22
4
  • lets say i am not using string ,rather i assign some object to argument as in argument=Some_class() even then its not working Commented Jun 20, 2010 at 14:25
  • this mechanism is often called "copy by reference", AFAIR even in some text books (but I can’t provide a reference now). Commented Jun 20, 2010 at 14:35
  • @Konrad: Interesting. I have updated my post to make the first paragraph clearer. Commented Jun 20, 2010 at 14:41
  • Python's model is called call by sharing - en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing. Commented Jun 20, 2010 at 15:32
1

it depends on if you are changing the referenced object itself (and if this object is mutable) or replacing the reference to another object. See the following example, which uses a mutable list...

>>> def test(arg):
... arg.append(123)
... 
... 
>>> s = []
>>> print s
[]
>>> test(s)
>>> print s
[123]
answered Jun 20, 2010 at 14:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.