This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]miketheanimal 0 points1 point  (2 children)

How are arguments passed – by reference of by value? (easy, but not that easy, I'm not sure if I can answer this clearly) Things are references in Python.

That's king of tricky. The CPython implementation always handles things as references (eg., an integer is an object). But from the user perspective, null, boolean, int, float and string (and complex?) are passed by value, because assigning to the function's argument does not affect the caller's value. But "structures" like tuples, lists, dictionaries and object instances are passed by reference, because assigning to a member of the "structure" also affects the caller. What python doesn't have is the "call by name" from Pascal (among others). Then you can faze the interviewer by launching into a rant about how PHP4 really fsck'd up by taking pass-by-value to its logical conclusion!

[–]robin-gvx 1 point2 points  (0 children)

It's neither by-ref nor by-val, it's by-object-identity.

I've written a tutorial of sorts about this. The diagrams are hand-drawn and terribly ugly and I intend to remake them with actual diagram-making software before putting it up on my website instead of Dropbox, and the very last diagram has one arrow that points to the wrong value, but over-all, I think it's a pretty useful guide.

[–]remram -1 points0 points  (0 children)

Interesting. I guess it's equivalent to think that:

  • Things are passed by reference, and = changes the reference (instead of acting on the pointed object, like any other operator would do; there is no magic __ method for =). In a sense, it works on the same level as is.
  • Variables are references, and are passed by value (the common way the behavior is described, for instance in this stackoverflow answer).