From the project
Practical guidelines for beautiful Python code
Every now and then Liraz and I find ourselves chatting about how much we love Python, but more so the lessons we have learned from coding, and how to apply them to create beautiful Python code. I've tried to refine some of the "lessons" into practical guidelines that I apply religiously to all new code I write, and the refactoring of old code I written.
When reading other peoples code it sometimes ties my mind into knots, and on occasions I want to pull my hair out in frustration and disgust. That's not to say I'm perfect, but hopefully these guidelines will benefit others (and indirectly help reduce my hair loss).
I couldn't possibly include everything I wanted to in one post, so this will be the first, and more will follow...
#1 - OO structure == Well defined mental concepts
#2 - Leverage built-in Python types
class IntField(int):
def __new__(cls, val, name):
return int.__new__(cls, val)
def __init__(self, val, name):
self.name = name
self.val = val
int.__init__(self, val)#3 - Use the class namespace, Luke!
- Enable inheritance - you can't override instance level attributes, only class level attributes.
- Readability - code is communication. Setting an attribute at the class level or instance level is making a statement about the nature of that attribute, which makes the code easier to read and understand.
#4 - staticmethod vs. classmethod vs. regular method
- If a method doesn't need access to instance level attributes then it should be a class method, not a regular method.
- If a method doesn't need access to class level attributes then it should be a static method, not a class method.
The Python Paradox
I haven't had the chance to take a look at tkldevenv yet. I'm closing up loose ends so we can finalize the 11.0 release. Once I do that I can come up for air.
I also owe you feedback on the tklpatches, haven't forgot about that.