Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> They want to know if function arguments are call by value or call by reference (neither).

My knowledge of Python is limited, but I was under the impression that it is call by value just like everything else (where Python's notion of value is "pointer to something"). Am I missing something?



You have it right in that it's call by value. But HN already had a fight over that a while ago: http://news.ycombinator.com/item?id=4783229 (That I apparently won. :P)


You essentially changed the definition of "value" to mean what is just called "name" in Python.

Names refer to objects. Names are introduced by name binding operations such as `=`, `import`, `def`.

  def f(a, b):  # a, b - local variables
       a = [1]  # doesn't change  x list; a refers to the new list hence forth
       b.append(2)  # change y list; b refers to the same list as y

  x, y = [], []
  f(x, y)
  print x, y  # [], [2]
Note: a "win" doesn't change the truth. It just means that nobody cared enough to participate further in the hostile discussion.


I see, people were mistaking the different nature of values in C and Python for a difference in calling semantics.


The answer -- regardless of who thinks they can "win" the argument -- is "this is not a useful question to ask, because people will draw incorrect conclusions, based on assumptions from other languages, from whichever answer is given".

Similarly, "pointer to something" is not a useful way to think about Python, because people with a background in C will start making assumptions about what "pointer" means. You might think that assigning to that "pointer" changes what's stored in the memory it points to, but really all it does is re-bind a name within the (local, which does not affect global) scope, leaving the "memory" unchanged.

So rather than ask which misleading-analogy-to-another-language Python uses, just ask how Python works.


I'm not sure I understand. Function calls work the same way in every call by value language: the binding of an argument to a value is the same as the binding of a variable or the assignment of part of a data structure. That's a pretty clear and simple thing to want to say, and "call by value" is the way to say it.

Since this is exactly the behaviour of Python's function calls, how is describing Python as call by value "not useful"? And where's this "misleading analogy to another language", given the concept is clearly explained without reference to any particular language?

I'm prepared to accept that people become confused over this, but I reject the notion that it is not a useful question.


People ask these questions because they're used to the way other languages work, and want to have an analogy to help them understand how Python works.

Except this isn't useful, because Python doesn't work the way those other languages do. They'll run into cases where their mental model of what "pass-by-value" or "pass-by-reference" means does not match up to Python's actual workings, and then they'll spend endless time arguing about which model Python uses when the answer is really "neither, stop trying to shoehorn one of those models onto a language that doesn't fit".




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

Search: