#π Embarrassing Question
45 messages Β· Page 1 of 1 (latest)
@regal nexus
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
What's your understanding of the two? This sounds like a homework question, so I'm hesitant to just give an answer here.
give this a read, it might help: https://nedbatchelder.com/text/names.html
It was originally from a past paper so revision not homework
thank you
I know the answer for the question, but understanding the question is different
I feel like ive just memorised an answer from the textbook from ages ago, without actually understanding whats happenign
A thing that confuses matters here is "pass by reference" has different meanings depending on the language. Python is essentially pass by value (meaning things are copied on passing), but the thing being passed is a pointer to the object, not the object itself.
No object is ever copied on assignment/passing, but a function also can't reassign variables outside of itself by reassigning its parameters.
Yeah, I've read about this from what I've seen, how Python is create different from e.g. C when talking about by values and by pararmeters
For context
This was the question on past paper
question ii)
Python basically works the same as Java and Javascript when it comes to their reference types.
Oh, this doesn't have anything to do with Python.
Yeah exam boards never refer to a specfic language, but I thought that this server may help
its mroe pseudocode
My intial thought process was that Python would have similar or the same kind of logic for this question if ygm
I have a question concerning this if it wont derail the OPs topic to much.
as Ive noticed in the same script file if i send a dict to a func ad dont return it, itll get updated, vrs sending it to a func in a mod file i would have to return it.
"Mod file"?
a file imported with the same function...
A function being in a different module changes nothing. No matter where a function comes from, copies are never made. If a imported function mutates its argument, that change will be seen externally.
!e
with open("mod.py", "w") as f:
f.write("def update(d): d['key'] = 'value'")
import mod
stuff = {}
mod.update(stuff)
print(stuff)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
{'key': 'value'}
TIL the bot has access to a filesystem.
it even sends pngs
so you can use matplotlib and save it to a png and it will send the graph
Oh, cool.
Back to the main question, "pass by value" just means a copy of the value is made on passing. "Pass by reference" though is a bit harder to define since it can vary a bit between languages. If they previously taught you C++, for example, they may expect a different answer than if they had taught you Java.
doesn't that just mean pass by reference?
"that" being what?
the thing being passed is a pointer to the object, we're passing a reference
I figured it out through linking it to a previous topic (I think I have anyway). Im kinda just making the connection that it works similiarly to how indirect and direct memory addressing works in LMC/Assembly for the CPU
yeah that makes sense in python, but thats where the "depending on the language" can come in, e.g.
void f(int &x) {
x += 1;
}
void g() {
int x = 0;
f(x);
// x = 1
}
Like this:
pass by reference β indirect addressing
and pass by value β direct addressing
Thats how Im kinda understanding it atm
pass by value would be mov'ing the value into the respective register / putting on the stack for the argument (so there would be no way for the function to mutate it in a way visible to the caller), pass by reference would be mov'ing its' address
i don't think i've seen this before, is this making x inside f refer to g's stack frame? it's like passing a pointer, but looks like a local variable on the surface
odd feature, i'm only used to passing pointers in c/zig and writing into the locations of those pointers
I might be giving C++'s definition too much weight because it was my first language. In most circumstances, "pass by reference" works as it does in Python and Java and such. I think C++ is the odd one out here where a reference allows reassigning the passed variable.
what language even is this?
The assignment says it's pseudocode.
My take:
In compiled languages, variables basically are space in memory. So pass by value means passing the value only and storing it in a new space.
Vs pass by reference is actually passing that same "space".
In python, variables are just names. They're akin to pointer to just any object but without being able to do pointer arithmetic. Reassigning stuff changes the stored address.
That's how the arrows in Ned's blog basically work, any var= changes where the arrow points.
Python's passing is sometimes referred to as "pass by assignment" because it follows exact rules of python assigning values to a variable (and without people being confused by different meanings of those "pass by" in other langs).
Meaning mutable stuff can be mutated... But arg1=something inside a function will be visible only in that function (because we change what we point to for that name only, not modifying the outside name).
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.