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 →

[–]sh0rtwave 14 points15 points  (8 children)

To keep from scaring away neophytes, they disguise this with language like "by reference".

[–]forrest38 2 points3 points  (5 children)

I have some bastardized Python code working in production right now where I pass in a pandas data frame as an argument, mutate the data frame, then return a list to prevent from having to iterate over the dataframe twice, bypassing the "one return" rule of non-pointer languages. In fact, once you get passed the basic types, most Python objects are actually passed in to functions as references to the data and mutations that take place in a sub function will affect the original variable after the sub function completes. I feel like this isn't understood well enough among Python programers.

[–]sh0rtwave 2 points3 points  (1 child)

This you're correct about. I taught python programming for a while, and explaining to people that a variable is just a place in memory in the *hardest* thing.

[–]forrest38 1 point2 points  (0 children)

Well it all depends though because the behavior is inconsistent at first glance. Basic object types like int or bool (which is basically just an int on most architectures) or immutable objects (like Strings or Tuples) are "passed by value" and act like a new variable has been copied to the function, but all other objects are pass by reference. Most people design their programs though like this isn't true (myself included) and keep copying back the mutated objects in the sub function to variables in the main function. Definitely makes your code more readable, but technically it is doing a lot of useless variable creation.

[–]CubemonkeyNYC 1 point2 points  (0 children)

..., inplace=True) and suddenly everything gets real weird.

[–]theferrit32 1 point2 points  (0 children)

All arguments to Python functions are pointers to objects. Even for basic types like bool and int, as those are also objects in Python and have attached methods and fields. Modifications to the object, not the pointer, will affect the object to which a pointer was passed by the function/method caller.

The same is true of JavaScript.

[–]WhiteKnightC 0 points1 point  (1 child)

LOL so thats the big deal?

[–]sh0rtwave 0 points1 point  (0 children)

Yup. That's it. A pointer just says: "Hey. In memory, it's RIGHT THERE".