When a programmer hasn’t used a language for a while, that language will update, sometimes with new syntax. Also, out-of-mind, out-of-memory. I haven’t touched Python since 2015, so it is refresher time. With studying through a book or video (I’m doing both), sometimes an experienced developer will jump the gun and start testing snippets of code on their own. In this case I wanted to see how Python handles copying lists. Something I should have mentioned yesterday was that I’m using Python 3.7.3. A line in yesterday’s code will not work in Python 3.5 or below.
The portion being refreshed is lists. Python is good with lists because items in that list can be inserted, changed, appended, sorted, reversed and popped like a stack. An issue that should be kept in mind with new coders is that these manipulations permanently modify the list. A glitch that modifies a large list can be hard to repair. Sometimes it’s better to construct filtered lists to make the program faster. In any event, keeping the original list untouched is prudent in most cases. But there’s always a but. This is something you should know regarding every language. Know how to copy a variable!
Python does not create a new object when you command B to equal A. Let that sink in. If you create a list called A and then command B = A, it does not create a copy of list A. Instead, it creates the new label B and points it to the same list as A. When you print A and you print B, it will print out the exact same list. When I was reading the chapter on lists and found out that sorting the list is permanent, I decided to write the following code. Having remembered this pointer issue, the purpose was to verify my suspicions.
o_cars = ['bmw','audi','volkswagen','subaru','mazda','kia'] s_cars = o_cars r_cars = o_cars s_cars.sort() r_cars.reverse() print(o_cars) print(s_cars) print(r_cars)
The goal is to make three lists, the first is the original list, the second is a sorted list, alphabetically ordered, and the third is a reversal of the original list. Here is the result:
['volkswagen', 'subaru', 'mazda', 'kia', 'bmw', 'audi'] ['volkswagen', 'subaru', 'mazda', 'kia', 'bmw', 'audi'] ['volkswagen', 'subaru', 'mazda', 'kia', 'bmw', 'audi']
As you can tell, this is not what I wanted to do. I told s_cars to equal o_cars and r_cars to equal o_cars. Then I sorted the order of s_cars and reversed the order of r_cars. All this time I was manipulating the same list. First it was sorted in alphabetical order, then it was reversed.
How can I make it work? Python has a copy() method. Instead of linking a variable to a pointer, just use the method.
o_cars = ['bmw','audi','volkswagen','subaru','mazda','kia'] s_cars = o_cars.copy() r_cars = o_cars.copy() s_cars.sort() r_cars.reverse() print(o_cars) print(s_cars) print(r_cars)
Now the result was correct.
['bmw', 'audi', 'volkswagen', 'subaru', 'mazda', 'kia'] ['audi', 'bmw', 'kia', 'mazda', 'subaru', 'volkswagen'] ['kia', 'mazda', 'subaru', 'volkswagen', 'audi', 'bmw']
Even an experienced programmer can overlook this. There are no tracebacks with this kind of error. Thinking like a coder will be second nature with experience.
I’m afraid to mention the book I’m reading after these two posts. There’s really nothing wrong with the book. When refreshing, it’s best to start at the beginning. A newer version of the language may change the syntax and sometimes the author may provide insight in a way I never thought about. With that said, the book is called, “Python Crash Course” by Eric Matthes, published by No Starch Press, 2nd Edition. So far it’s great. Advice for learning code: do not buy old, used books. I have a library of outdated books, including my old “Learning Python” from O’Reilly & Associates in 1999, version 1.5.2 of Python. Programming books make bad investments.
Part of my refreshing in Python includes learning game programming with the language. Games are powerful projects because the logic necessary provides a vehicle for writing advanced algorithms. These algorithms can also help with simpler programs that may benefit from better coding. Games require speedy execution. A simpler program may be lenient for poor coding technique. Writing games will make you a better coder without a doubt.
I originally used Python to manipulate text. In the beginning, Perl was my choice language while a Unix engineer. Many Perl scripts were simple client-server programs that ran in the background, sending monitoring information, like a precursor to Nagios. I also wrote Nagios and Netcool probes in Perl as well. While Python seems to be the “it” language for web development, it would be fun to explore old Perl scripts in Python as well. I have the bug for programming again.