When you make a tutorial how to write better code in Python, you should follow, you know, Python conventions how to write code. http://www.python.org/dev/peps/pep-0008/#whitespace-in-expre... The same with those oneline conditionals, lack of spacing between functions, etc. Following PEP8 (with --ignore=E5,W602) is useful (if not for you, then for others who will commit to your code) and easy (configure your ide).
I ignored pep8 for a long time, until I noticed more and more whitespace mess in diffs. Following pep just makes work easier.
I get it. PEP-8 is rad, and pretty well accepted... but:
1. It's a blog post. It's subjective opinion. 2. Whitespace alignment is rad, and some of the PEP-8 whitespace rules are pretty unfortunate.
I guess if it were me I'd at least have called out that the alignment violates PEP-8 as a warning to folks.. and I wouldn't have led the tutorial with it, so that I'd at least be dealing with trolls that make it through the entire post.
my_data = 3
your_data = 2
much easier to skim than my_data = 3
your_data = 2
Given that PEP 8 even says "But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best." I have no problem occasionally using more than one space for allignment.It feels wrong that GameWorld should keep track of a list of powerup types independently from instances of the Powerup class, and that Powerup instances have no knowledge of what they do. It smells suspiciously like using strings as a substitute for object types. To me, that hints that there are stronger refactorings to be found that could take advantage of polymorphism or a strategy pattern, depending on your taste for inheritance hierarchies.
However, I find that this is particularly helpful in that it based on an actual working code base, instead of contrived examples. It demonstrates that writing clean code isn't just a pointless academic exercise, there is a purpose to it.
def wrap_around(self):
"""Change the position of the bubble to toroidally
"wrap around" if it goes off one edge of the
map."""
if self.pos.x < 0: self.pos.x += 1
if self.pos.y < 0: self.pos.y += 1
if self.pos.x > 1: self.pos.x -= 1
if self.pos.y > 1: self.pos.y -= 1
Why isn't else used? In addition to efficiency it tells the reader that the author expects at most one of x++ or x-- to happen.