#πŸ”’ Why would I want to copy a function?

67 messages Β· Page 1 of 1 (latest)

modest nymph
#

I'm confused as to why you would want to since after defining a function you call back on it and nothing inside the function changes? I don't understand the utility of it at the moment or how it works from the examples I've seen, and it hasn't said anywhere what the point of the [:] is for. I'm not sure how this is supposed to differ from a normal function

gloomy tulipBOT
#

@modest nymph

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.

crisp stratus
sturdy trench
#

it isn't copying the function, it's copying the list

modest nymph
#

Oh

modest nymph
#

still [:]?

crisp stratus
#

Was getting on my computer

#

Yeah

#

It should work

modest nymph
#

but how would you access the first copy again

#

after you made a 2nd copy?

crisp stratus
#
copy1 = original_list[:]
copy2 = original_list[:]
#

It would have to be something like this

modest nymph
#

Right, how didn't I think of that

crisp stratus
fast terrace
#

The initial example is a bit complicated. I don't know why it uses a function.
I think this is easier:

my_list = [10, 20, 30] # Create a list
my_copy = my_list[:] # Copy the list
my_copy[1] = 99 # Modify the copy
print(my_list) # The original list isn't modified```
modest nymph
small timber
#

I suspect the intent of using a function is to show that Python variables are references to objects. Passing a list to a function does not make a copy for the function to work on.

fast terrace
#

But that doesn't matter in the specific example because it passes a copy of the list.
Maybe there's a second example that passes the original list?

modest nymph
#

Theres no follow up

small timber
#

Shrug. Speculating about the intent behind something unexplained is always guesswork.

modest nymph
#

in fact the previous example is just the same one except without copying it

small timber
#

Right. So you've got a function that modifies a list in place. Ifyou didn't want your original modified a copy needs to be made, either to be worked on or to preserve the contents which are about to be modified.

modest nymph
#

What do you mean by in place?

crisp stratus
#

The function changed the second value

#

[1] being changed to 99

small timber
#

This:

sorted(my_list)

returns a new list containing the elements of my_list, sorted into some order.
But this:

my_list.sort()

rearranges the elements in my_list inside the list itself. Thus "in place".

modest nymph
#

that last sentence is confusing me

#

oh wait

#

I think I get what you're saying

#

it doesnt require any external code for the 2nd one to work right?

small timber
#

Set my_list = [1, 2, 3, -1] and so a fe experiments, printing my_list.

#

No. The list type has a .sort() method presupplied.

#

So "right".

modest nymph
#

I'm kind of lost again

#

the presupplied sort method does what exactly?

small timber
#

It sorts the element of the list in place. The original list is rearranged.

#

!e

sturdy trench
#

it's showing you that copying a list creates a new list by modifying one of the lists

#

sorry, discord didnt load some messages for a bit

small timber
#
>>> L1 = [1, 2, 3, -1]
>>> L2 = L1    # just a nother reference to the same list
>>> L3 = L1[:] # a copy of the original list, a new list
>>> L1
[1, 2, 3, -1]
>>> L2
[1, 2, 3, -1]
>>> L3
[1, 2, 3, -1]
>>> L4 = sorted(L1)
>>> L1
[1, 2, 3, -1]
>>> L4
[-1, 1, 2, 3]
>>> L3
[1, 2, 3, -1]
>>> L3.sort()
>>> L3
[-1, 1, 2, 3]
#

Note that we did not assign anything new to L3. It points at the original copy it was given. The copy has been rearranged.

modest nymph
#

How would L1 change if you didnt copy it for 3? Isn't L3 a new variable so it wouldn't affect the original? Just use it as a base? or is it a effiency reason?

small timber
#

If I went L3 = L1? That makes another reference to the original list. L1, L2 and L3 would all refer to the same, original list.

#

So L3.sort() would sort the original list.

modest nymph
#

Why?

#

that doesnt make sense to me, its supposed to be its own variable now right? A copy in a sense without that?

sturdy trench
#

"Speaker: Ned Batchelder

The behavior of names and values in Python can be confusing. Like many parts of Python, it has an underlying simplicity that can be hard to discern, especially if you are used to other programming languages. Here I'll explain how it all works, and present some facts and myths along the way. Call-by-reference? Call-by-v...

β–Ά Play video
#

i think that video will answer all your questions about this

modest nymph
#

Huh

small timber
#

Python variables do not contain anything except a reference to an object.

modest nymph
#

Can you copy other things besides lists?

small timber
#

That's part of the (inferred) reason for the function in your examples: they show that the function can modify the original object (the list) which was passed to it.

#

Anything can be copied. That can mean many things. In fact, there's an entire copy stdlib module for complicated situations.

modest nymph
#

That makes a lot more sense now

small timber
#

Using references means that assignments are always basicly free. They're just copying a reference.

#

The flip side is that when you need a distinct copy for whatever reason, you need to make one.

modest nymph
#

Somehow I never made that connection, I would know it would modify a variable when I referred to it making another assignment = to it's assignment yet I forgot that now

small timber
#

Draw little boxes with arrows. It helps. - Michael J. Eager

#

An assignment makes a new arrow.

#

Calling a function foo(x) assigns x to whatever the function parameter is. Just an arrow.

gloomy tulipBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.