+ 3
[Solved] What is the correct terminology here? (by reference vs by value vs by object reference) (py)
def funcA() : v = 10 print(funcB(v)) def funcB(arg) : arg = 0 print(arg) funcA() Do I say that funcB is passed the variable v, or value v? I apologize to anyone who might have seen the very long messed up version of this question and set out to answer. I realized I had to really simplify and clarify it. Thanks. Also: I thought I'd got it but I don't think I truly understand passing by value or by reference or by object reference in all their implications. I still get confused like this. Me study OOP?(I have 5 chapters before that, I want to just skip them... )
6 Answers
+ 3
This was an interesting enough topic for me to dig a little deeper.
Python treats everything as an object, even numbers. So technically it is always passing a memory reference in function calls.
https://code.sololearn.com/chFLKMsYyVBc/?ref=app
The main concern is, if those objects are mutable or immutable. Obviously an int is immutable, so is a string, a tuple or a frozenset. But passing lists to functions, we can do mutations inside the function, to objects that are technically outside of the function scope.
This is where things can get complicated :)
https://www.scaler.com/topics/call-by-value-and-call-by-reference-in-python/
+ 5
when passing a value to a function, you would call that an argument.
so you could say that funcA passes V as agument to funcB
also, V is a variable, not a value. 10 would be the value.
I hope this helps :)
+ 3
Tibor Santa Thanks a bunch, it's been some time I asked this, I know, and because I exhausted my comment space under your code by nonstop questions, and it'd make perfect sense for you to avoid them, I am using this space now to ask a single question to replace the ones there that would only work if you read them(totally get it if you don't, it's too much and thanks a lot):
Is there a source particularly about these subjects that's not Pro stuff? Does SICP dwell on such things?
+ 1
Apollo-Roboto
Thank you. There's actually a LOL moment I'd like to share:
In the original function definition from the book v was named "value".
A variable named as "value" . And the book was referring to this variable both as "variable" and "value" đč. Now, although "value" was written slightly in bold, I'd missed that detail and I sent myself into a mindfudge tornado.
hahahaha brutal.
If you could only see my first question and the lengths I'd gone to explain my problem....
This is passing as object by reference, all the same. Python is that way.
Yes I am passing a variable, but as value.
So when a book calls that variable itself "value", too... late at night towards dawn... brutal lol. (I still need to rush to that chapter)
+ 1
Korkunç el Gato my favourite source is RealPython.
They have some really good articles about almost any python topic.
https://realpython.com/python-memory-management/
I think SICP is more focused on software architecture as a whole, and it's using Lisp (Scheme) language for examples, but to be fair, I have not finished it yet, so I am not sure if this topic is covered.
+ 1
Tibor Santa Noted, thank you. For those who are interested in this there's also a Python documentation link that Tibor Santa shared under the code that he prepared for this question, a link that dwells on how memory allocation works in Python.