[Tutor] Query: lists

Alan Gauld alan.gauld at yahoo.co.uk
Tue Aug 14 19:23:36 EDT 2018


On 14/08/18 22:38, Cameron Simpson wrote:

> If you're trying to partition words into values starting with "x" and values 
> not starting with "x", you're better off making a separate collection for the 
> "not starting with x" values. And that has me wondering what the list "b" in 
> your code was for originally.

And further to Cameron's point this demonstrates why choosing
meaningful variable names (rather than single letters) is so
important. If a,b and z had been names expressing their purpose
we would be better able to guess at your intentions and
ultimate goal. But with single letters we have no real clue.

> As a matter of principle, functions that "compute a value" (in your case, a 
> list of the values starting with "x") should try not to modify what they are 
> given as parameters. When you pass values to Python functions, you are passing 
> a reference, not a new copy. If a function modifies that reference's _content_, 
> as you do when you go "words.move(z)", you're modifying the original.

It's also good if functions that compute a value *return* that
value rather than (or as well as) print it. The act of returning
something also helps to clarify the functions objective. With
multiple print statements we are not quite sure which line of
output is the most important.


-- 
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




More information about the Tutor mailing list