A very important guideline for writing software is to keep your code "DRY". DRY is an acronym for Don't repeat yourself. But if you think back to the lesson on colors, we repeated ourselves quite a bit when creating that color bar. To refresh your memory, I've copied over the codeblock from that lesson:
As it turns out, there is a better way; iterating over lists. The goal of this lesson is to DRY up the piece of code above. Before we get to it, let's learn how to iterate over lists.
for
loopsfor
loops are the best way to iterate over lists. Let's illustrate this
using a simple example:
If you hit that Run button, then you know that the for
loop construct
allows you to run a block of Python code for each item in a list. In the above
example, that block of code happens to be a simple print
statement. One can
imagine a similar piece of code where that block is a place
function instead
of a print
function:
You may have noticed that I didn't bother wrapping the range
call in a
list
. range
is special that way and I'll explain that in a future
lesson. For now, just remember that we can loop over a range
without
using a list
.
If we just construct a list of colors at the top of the script, we can then easily iterate over them and place them. Let's see how that could work:
One problem here is that the color bar starts at the coordinates 0, 0, 0
. We
want the bar to be centered around 0, 0, 0
instead. A simple solution is to
subtract half the length of the color bar from the x
coordinate when
place
-ing.
We could just count the number of items in the list_of_colors
and calculate
the amount we need to subtract as 11
. But that would mean that our program
is not flexible. Each time you add a new color to the list_of_colors
, you'll
have to adjust the index offset. It would be better if we could consider
list_of_colors
the only source of information and calculate everything based
on it.
A good way to calculate half the length of the color bar is to use
len(list_of_colors)/2
, but if the number of elements in the list is odd, you
won't end up with an integer. There are a lot of ways to solve this problem. Let's
explore a few:
A relatively recent feature of Python allows us to do "floor division" and that's probably the most elegant way to solve the issue. So let's see how we can use this in practice:
A further simplification can be made by using the ALL_COLORS
list provided
by ctb
instead of constructing our own. Let's see what this simplification
looks like:
We've now managed to create the color-bar using a much more readable and maintainable version of the original code. Try updating the code in the source pane above so it creates the mirror image of the color-bar (the first and last colors are flipped, the second and the second-last colors are flipped etc.)
In the next tutorial, we'll be using using this knowledge of loops along with some geometry to create a 3D ring. Stay tuned!
© 2024 Abhin Chhabra | Sitemap