Posts: 279
Threads: 107
Joined: Aug 2019
Here's the end of a short program I wrote:
targetm = []
def chop(lst):
tgti = len(lst) - 1 #this is the last item index in the list
del lst[tgti] #delete the last item
del lst[0] #delete the first item
def middle(targetm):
targetm = target[1:len(target)-1]
choplist = chop(targetc) #targetc is a copy of list "target", which was created earlier by user input
middlelist = middle(targetm)
print('New list generated by chop function is',choplist)
print('New list generated by middle function is',middlelist) Chop is supposed to delete the first and last items in a list and return None.
Middle is supposed to return the original list without the first and last items.
Program output is as follows:
Enter value for list. If done, enter "done": a
Enter value for list. If done, enter "done": e
Enter value for list. If done, enter "done": i
Enter value for list. If done, enter "done": o
Enter value for list. If done, enter "done": u
Enter value for list. If done, enter "done": done
Created list is ['a', 'e', 'i', 'o', 'u'] .
The list has 5 members.
New list generated by chop function is None
New list generated by middle function is None
I have two questions:
1. Why _does_ chop return None?
2. Why _doesn't_ middle simply remove first and last items of the list?
Thanks!
Mark
Posts: 2,168
Threads: 35
Joined: Sep 2016
Aug-30-2019, 03:15 PM
(This post was last modified: Aug-30-2019, 03:15 PM by Yoriz.)
Your function are not using return to return something useful, by default a function will return None.
Posts: 279
Threads: 107
Joined: Aug 2019
Great!
One further question. I understand lists are mutable, but if I copy the list and work on the copy then why is the original list affected? Check out this snippet:
targetc = target #make copy of original list
print('Copy of original list is', targetc)
def chop(lst):
tgti = len(lst) - 1 #this is the last item index in the list
del lst[tgti] #delete the last item
del lst[0] #delete the first item
return(lst)
print('New list generated by chop function is',chop(targetc)) #performing function on copied list
print('Original list is now',target)
quit() The output is:
Enter value for list. If done, enter "done": a
Enter value for list. If done, enter "done": e
Enter value for list. If done, enter "done": i
Enter value for list. If done, enter "done": o
Enter value for list. If done, enter "done": u
Enter value for list. If done, enter "done": done
Created list is ['a', 'e', 'i', 'o', 'u'] .
The list has 5 members.
Copy of original list is ['a', 'e', 'i', 'o', 'u']
New list generated by chop function is ['e', 'i', 'o']
Original list is now ['e', 'i', 'o']
Posts: 1,358
Threads: 2
Joined: May 2019
Line 1 does not make a copy of the list. It just creates another variable pointing to the original list.
In another thread one of the better programmers here posted a link that really helps to explain this (I think yesterday). Watch the video, it will become clear.
How variables work in Python
Posts: 2,168
Threads: 35
Joined: Sep 2016
Aug-30-2019, 04:10 PM
(This post was last modified: Aug-30-2019, 04:11 PM by Yoriz.)
targetc = target #make copy of original list does not make a unique copy of a list, it just has another variable with a pointer to the same list.
https://docs.python.org/3/tutorial/datas...e-on-lists Wrote:list.copy()- Return a shallow copy of the list. Equivalent to a[:].
targetc = target.copy() or
targetc = target[:] Also see
https://docs.python.org/3/library/copy.html Wrote:copy — Shallow and deep copy operations
Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. This module provides generic shallow and deep copy operations (explained below).
Interface summary:
copy.copy(x)- Return a shallow copy of x.
copy.deepcopy(x[, memo])
exception copy.error- Raised for module specific errors.
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Two problems often exist with deep copy operations that don’t exist with shallow copy operations:
Recursive objects (compound objects that, directly or indirectly, contain a reference to themselves) may cause a recursive loop.
Because deep copy copies everything it may copy too much, such as data which is intended to be shared between copies.
The deepcopy() function avoids these problems by:
keeping a memo dictionary of objects already copied during the current copying pass; and
letting user-defined classes override the copying operation or the set of components copied.
This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the pickle module.
Shallow copies of dictionaries can be made using dict.copy(), and of lists by assigning a slice of the entire list, for example, copied_list = original_list[:].
Classes can use the same interfaces to control copying that they use to control pickling. See the description of module pickle for information on these methods. In fact, the copy module uses the registered pickle functions from the copyreg module.
In order for a class to define its own copy implementation, it can define special methods __copy__() and __deepcopy__(). The former is called to implement the shallow copy operation; no additional arguments are passed. The latter is called to implement the deep copy operation; it is passed one argument, the memo dictionary. If the __deepcopy__() implementation needs to make a deep copy of a component, it should call the deepcopy() function with the component as first argument and the memo dictionary as second argument.
Posts: 279
Threads: 107
Joined: Aug 2019
That makes sense... thanks for the help!
Posts: 279
Threads: 107
Joined: Aug 2019
Maybe I don't fully get it yet. In Charles Severence's _Python for Everybody_ book, he includes this example:
https://www.screencast.com/t/txn9O0ozu
Going back to my initial example where I failed to use RETURN, why, here, is RETURN the difference between changing the list or not doing so?
I almost want to think a defined function without a return line won't ever do anything, but I'm guessing an experienced programmer could give a list of examples where this is false.
|