Hacker News new | past | comments | ask | show | jobs | submit login

You can do that if the parameter is a list:

def change(l):

     l[1] = 'x'

 >>> foo = [1,2,3]

 >>> foo

 [1, 2, 3]

 >>> changel(foo)

 >>> foo

 [1, 'x', 3]



This does a fundamentally different thing with similar syntax. Assignment in Python is fundamentally a rebinding operation; slice assignment rebinds a reference internal to the object, whereas ordinary assignment rebinds the name.

The classic test: in Python you cannot write a generic "swap function" - you could only possibly write a function to swap the internal states of the arguments, presuming them to be compatible.


You can't change which list `foo` refers to, though. Only the contents of that list.

`foo` is a reference to a list, and that reference is passed by value.


Now make foo point to another list, not change contents of the list foo points to.

Passing by reference requires changing the variable itself, used on the call site.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: