Re: [Tutor] basic decorator question
On Mon, Jul 24, 2017 at 11:01 AM, Steven D'Apranowrote: > > There's more to decorators than that, but hopefully that will > demonstrate some of the basic concepts. Feel free to ask any more > questions on the mailing list, and we will answer if we can. > I hope I can ask questions, too! ~(:>)) I am having a recurring problem with Python: I can work out the mechanics of how various Python constructs work, such as decorators, but then I find myself scratching my head as to why would I want to use them. The idea of replacing a function with its decorated version sounds cool, but what types of problems would I want to use this approach on? One thing that bothers me, is that once I decorate the function, I no longer have access to the original, un-decorated function. But on the other hand, if I had a decorator which I wanted to apply to multiple functions, then I would be DRY-er by taking this approach -- I would need only one decorator function and could then use it decorate as many other functions as it made sense to do so. So some guidance, please, on what types of problems this approach is useful for? TIA! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the difference between sort(aList) and aList.sorted()
On Wed, Jul 26, 2017 at 10:03:59PM -0400, C W wrote: > Thank you very much, all! > > One other question: how do you look up a method? Any of these will work: help(list.sort) help([].sort) alist = [1, 2, 3, 99] help(alist.sort) -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the difference between sort(aList) and aList.sorted()
On Wed, Jul 26, 2017 at 02:40:17PM -0400, C W wrote: > sorted(aList) > > [2, 3, 4, 5] sorted() makes a copy of whatever you give it, as a list, and sorts the copy. It doesn't have to be a list to start with: py> sorted("alphabet") ['a', 'a', 'b', 'e', 'h', 'l', 'p', 't'] > aList.sort() > aList > > [2, 3, 4, 5] The sort() method only works on actual lists, and it sorts the list in place, just as list.reverse() reverses the list, list.append() appends a value to the list, list.insert() inserts a value into the list, etc. > Why is there both? They do the same thing. Is if I unknowingly hit the > keyboard with the aList.sort(), then the "damage" is permanent. They don't do the same thing. sorted() makes a copy of the argument first, list.sort() does not. We have both because sometimes one is useful and other times the other is useful. Originally, and for many years, Python only had list.sort(), and if you wanted to sort a copy you had to write: blist = list(alist) blist.sort() which is always a minimum of two lines and not very convenient. So it was eventually decided to add sorted(). -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the difference between sort(aList) and aList.sorted()
On 26/07/17 19:40, C W wrote: > My understanding of each is: > 1) function(variable) is manipulating a vector, I can do bList = > sorted(aList) > 2) object.method() is permanently changing it, I don't even need to assign > it in #1. > > Why is there both? They do the same thing. As you have just shown they do very different things. One sorts the list in place the other returns a sorted copy of the list. Sometimes you want one, sometimes the other. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the difference between sort(aList) and aList.sorted()
On Wed, Jul 26, 2017 at 2:40 PM, C Wwrote: > Dear Python experts, > > I suppose I have the following Python code: > > aList = [3, 5, 2, 4] > > sorted(aList) > > [2, 3, 4, 5] > > aList.sort() > > aList > > [2, 3, 4, 5] > > My understanding of each is: > 1) function(variable) is manipulating a vector, I can do bList = > sorted(aList) > 2) object.method() is permanently changing it, I don't even need to assign > it in #1. > > Why is there both? They do the same thing. Is if I unknowingly hit the > keyboard with the aList.sort(), then the "damage" is permanent. > > Thank you! > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > I haven't looked it up recently, but one sorts in place (object.method) i think, and the other (a function) returns a new sorted list -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] What's the difference between sort(aList) and aList.sorted()
Dear Python experts, I suppose I have the following Python code: aList = [3, 5, 2, 4] sorted(aList) > [2, 3, 4, 5] aList.sort() aList > [2, 3, 4, 5] My understanding of each is: 1) function(variable) is manipulating a vector, I can do bList = sorted(aList) 2) object.method() is permanently changing it, I don't even need to assign it in #1. Why is there both? They do the same thing. Is if I unknowingly hit the keyboard with the aList.sort(), then the "damage" is permanent. Thank you! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] new to python
On 7/25/2017 12:43 AM, Alan Gauld via Tutor wrote: On 25/07/17 04:58, N6Ghost wrote: this code works f = open("C:/coderoot/python3/level1/inputfile.txt", 'r') for line in f: for line in f: #print(line.rstrip()) print(line) f.close() the out put skips the first line of the inputfile and puts a blank line inbetween I'm not sure why you have two for loops? Why did you do that? Can you explain your thinking there? Remove one of the for... lines. Your code does this: f = open("C:/coderoot/python3/level1/inputfile.txt", 'r') open the file and assign it to 'f' for line in f: get the first line from f and assign it to 'line' for line in f: print(line) get the next line from f and assign it to 'line' This overwrites the value from the first for loop above. The line is then printed. The second loop then repeats for all of the remaining lines in the file. At the end of the second for loop control returns to the top for loop. But, since the file is now empty, the top loop never gets any more values from f, so it terminates. The blank lines are caused by the fact that the lines in the file end in a newline character and print() adds a newline of its own. Either reinstate your rstrip() call or stop print() adding a newline with print(line, end='') I'm also not sure why you posted two copies of your code? I assume you only use one since otherwise you would have told us that you got two lots of output? HTH final working code for this method: # open meth 1 f = open("C:/coderoot/python3/level1/inputfile.txt", 'r') for line in f: #print(line.rstrip()) print(line.strip()) j = line #print ("below new var used") #print (j) f.close() thanks for the replys, not sure why i had the second for loop. removing that remove some of the oddball behavioral issues. next going to look into using the with ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor