Re: [Tutor] 'in-place' methods
[1] Every couple of years, I decide to learn Java, and start going through a book -- usually the same book. It doesn't go long before I say to my self, Gosh, why would I ever want to program this language, anyway? I've taught myself Java three times(*), first from the O'Reilly Nutshell book and then from their 'Learning Java' book (very good, I recommend it) and now from 'JSP for Dummies' - which is not really Java but uses it a lot... Unfortunately I have to read a lot of Java at work and very occasionally actually program in it, but I do not like it at all. But I prefer it to either COBOL or Perl... (*)I did the same with SmallTalk but I quite liked it, it just was too big a paradigm shift in style from Lisp, Object Pascal and C++ first time around. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'in-place' methods
Michael Broe said unto the world upon 17/02/06 03:57 PM: snip Second question. Do I really have to write the sort_print() function like this: def sort_print(L): L.sort() print L i.e. first perform the operation in-place, then pass the variable? Is this the idiomatic way of doing it? Hi Michael, others have answered what you asked; I thought I'd try to head off a potential problem for you. Perhaps you've seen this already, but since you are wrapping the print in a function, I suspect you want the original list to be unmodified. Thus, compare: def sort_print1(a_list): a_list.sort() print a_list def sort_print2(a_list): t = list(a_list) t.sort() print t list1 = [sort_print1, mutates, the, original] list2 = [sort_print2, doesn't, mutate, the, original] sort_print1(list1) ['mutates', 'original', 'sort_print1', 'the'] list1 ['mutates', 'original', 'sort_print1', 'the'] sort_print2(list2) [doesn't, 'mutate', 'original', 'sort_print2', 'the'] list2 ['sort_print2', doesn't, 'mutate', 'the', 'original'] HTH, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'in-place' methods
Perhaps you've seen this already, but since you are wrapping the print in a function, I suspect you want the original list to be unmodified. Thus, compare: def sort_print1(a_list): a_list.sort() print a_list def sort_print2(a_list): t = list(a_list) t.sort() print t list1 = [sort_print1, mutates, the, original] list2 = [sort_print2, doesn't, mutate, the, original] sort_print1(list1) ['mutates', 'original', 'sort_print1', 'the'] list1 ['mutates', 'original', 'sort_print1', 'the'] sort_print2(list2) [doesn't, 'mutate', 'original', 'sort_print2', 'the'] list2 ['sort_print2', doesn't, 'mutate', 'the', 'original'] this is a slight tangent to mike's original question but is *another* gotcha for newbies who are swimming in the new waters of Python objects. and that is, that although Brian appears to make a copy of the original list in sort_print2() using the list() factory built-in function, it is only (called) a shallow copy. bear in mind that if you use list() to generate a(nother) list object, you are only copying references or aliases to the objects in the original data structure (here it is also a list), whether mutable or otherwise; but if it is the former, then be aware that you are NOT MAKING A COPY of those mutable objects, just references. those objects can then be modified whether you access the original or the one in the shallow copy: def sort_print2(a_list): t = list(a_list) t[-1][-1] = 'Yes' t.sort() print t foo = [ 'Have I been trumped?', 'No' ] myList = [ 456, 'abc', 1.23, foo ] sort_print2(myList) [1.23, 456, ['Have I been trumped?', 'Yes'], 'abc'] myList [456, 'abc', 1.23, ['Have I been trumped?', 'Yes']] see how i'm modifying 't' and not 'a_list'. changing either one will have the exact same outcome because they both contain a reference to the same object 'foo'. in other words, no copy of 'foo' was made, so that's why it's affected. if you really need a complete copy (of objects *and* the ones contained within recursively), then read up on the deepcopy() function of the copy module. (caveat tho, that is doesn't copy EVERYTHING, but enough to make you feel like it does. for example, it doesn't copy files, functions/methods, stack frames, etc.) cheers, -wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Core Python Programming, Prentice Hall, (c)2006,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'in-place' methods
On Fri, 17 Feb 2006, Kent Johnson wrote: Ruby has an interesting approach to this - the names of mutating methods end with !. So it would be list.sort!() which gives a strong cue to what is happening. What a great idea! Hmm.. maybe Python could do this, too; and use some other characters like $, @ and % to indicate if a name is a reference to a plain old variable, a list, or a dictionary! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'in-place' methods
Ruby has an interesting approach to this - the names of mutating methods end with !. So it would be list.sort!() which gives a strong cue to what What a great idea! Hmm.. maybe Python could do this, too; and use some other characters like $, @ and % to indicate if a name is a reference to a plain old variable, a list, or a dictionary! :-) But don't even joke about it. Decorators are bad enough, no more line noise in Python. If we want weird characters in our code we can just learn Perl! I assume this was intended irony Terry? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'in-place' methods
On Sun, 19 Feb 2006, Alan Gauld wrote: But don't even joke about it. It is pretty cringe-worthy, isn't it? Decorators are bad enough, no more line noise in Python. I was actually kind of sad to see that added. It struck me as the first grafted-on feature of Python that *felt* grafted-on. Fortunately, I'm not at the level of programming that I'm ever likely to run into it, so I can safely ignore it. Another one like this: the Generics feature of Java 5. Not that I care much for Java, anyway,[1] but yech. If we want weird characters in our code we can just learn Perl! I assume this was intended irony Terry? Absolutely. I'm a Perl refugee. [1] Every couple of years, I decide to learn Java, and start going through a book -- usually the same book. It doesn't go long before I say to my self, Gosh, why would I ever want to program this language, anyway? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor