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
#π Why would I want to copy a function?
67 messages Β· Page 1 of 1 (latest)
@modest nymph
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.
Closes after a period of inactivity, or when you send !close.
Doing [:] makes a copy of the list so any change done to my_list[:] won't be reflected in the original my_list, but I don't really get your question here like you don't understand the use?
it isn't copying the function, it's copying the list
Oh
If you have multiple copies of the same list what do you put?
still [:]?
Ok sorry for the delay
Was getting on my computer
Yeah
It should work
copy1 = original_list[:]
copy2 = original_list[:]
It would have to be something like this
Right, how didn't I think of that
Ahaha no worries same icl
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```
Theres been a lot of explantions and examples that dont really make sense in hindsight, or require you to use something that it didnt go over yet for some reason
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.
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?
Theres no follow up
Shrug. Speculating about the intent behind something unexplained is always guesswork.
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.
What do you mean by in place?
I think they mean the index of the list
The function changed the second value
[1] being changed to 99
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".
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?
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".
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
>>> 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.
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?
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.
Why?
that doesnt make sense to me, its supposed to be its own variable now right? A copy in a sense without that?
"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...
i think that video will answer all your questions about this
Huh
Python variables do not contain anything except a reference to an object.
Can you copy other things besides lists?
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.
That makes a lot more sense now
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.
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
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.
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.