#πŸ”’ Embarrassing Question

45 messages Β· Page 1 of 1 (latest)

regal nexus
#

Genuinely embarrassed to ask this question but I feel like my brain is about to explode over the simple ahh topic:

What is passing by value and passing by reference.

How does passing a parameter differ between the two. I know its such an easy thing, I think it may just be the fact im on 2hours of sleep icl ahah

regal sailBOT
#

@regal nexus

Python help channel opened

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.

timid cypress
#

What's your understanding of the two? This sounds like a homework question, so I'm hesitant to just give an answer here.

regal nexus
regal nexus
#

I feel like ive just memorised an answer from the textbook from ages ago, without actually understanding whats happenign

timid cypress
#

No object is ever copied on assignment/passing, but a function also can't reassign variables outside of itself by reassigning its parameters.

regal nexus
#

For context

#

This was the question on past paper

#

question ii)

timid cypress
#

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.

regal nexus
#

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

trail crow
trail crow
#

a file imported with the same function...

timid cypress
#

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.

stone kelp
#

!e

with open("mod.py", "w") as f:
  f.write("def update(d): d['key'] = 'value'")

import mod
stuff = {}
mod.update(stuff)
print(stuff) 
regal sailBOT
timid cypress
#

TIL the bot has access to a filesystem.

stone kelp
#

it even sends pngs
so you can use matplotlib and save it to a png and it will send the graph

timid cypress
#

Oh, cool.

timid cypress
glossy pivot
stone kelp
#

"that" being what?

glossy pivot
#

the thing being passed is a pointer to the object, we're passing a reference

regal nexus
#

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

stone kelp
regal nexus
#

Thats how Im kinda understanding it atm

stone kelp
#

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

glossy pivot
#

odd feature, i'm only used to passing pointers in c/zig and writing into the locations of those pointers

timid cypress
# glossy pivot doesn't that just mean pass by reference?

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.

pine dew
timid cypress
molten moth
#

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).

regal sailBOT
#
Python help channel closed for inactivity

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.