And I realized that Python had learned a lot from these economists.LUKE: Is from module import * better than explicit imports?
YODA: No, not better. Quicker, easier, more seductive.
LUKE: But how will I know why explicit imports are better than the wild-card form?
YODA: Know you will when your code you try to read six months from now.
It's often dificult for a programmer to see the advantage of doing something the right way, which will be benneficial in six months, over just getting something done now. However, Python enforces doing things the right way, and when doing things the right way is just as easy as doing in the wrong way, you make the intuitive decision of doing things the right way. Almost every code base I've worked with(outside of Python) had some basic indentation rules that the code observed, Python just encodes this into the language, which requires all code to have a certain level of readability.
Django has also learned this lesson. For example, the template language flat out prevents you from putting your business logic inside of it without doing some real work, you don't want to do that work, so you do things the right way and put your business logic in your views. Another example would be database queries, in Django it would be harder to write a query that injected unescaped into your SQL than it would be do the right thing at use parameterized queries.
Ultimately, this is why I like Python. The belief that best practices shouldn't be optional, and that they shouldn't be difficult creates a community where you actively want to go and learn from people's code. Newcomers to the language aren't encouraged to "just get something working, and then clean it up later," the communiity encourages them to do it right in the first place, and save themselves the time later.
While I'm glad you like it, I don't want to claim credit falsely. The Luke/Yoda exchange didn't originate with me, I just adapted it. Here's the original:
ReplyDeletehttp://www.python.org/doc/humor/#python-vs-perl-according-to-yoda
I'll add that link to my text.
view logic can be written in Python too, and a little bit of that in templates is OK. django templates are too restrictive in this regard.
ReplyDelete> However, Python enforces doing things the right way
ReplyDeleteSorry, I think it is common knowledge that Python will let you get away with all sorts of shenanigans if you want to. (There is passing reference to this in one of the Python O'Reilly books).
Python is very hacker-friendly - please don't mistake top-notch syntax and good organization for imposing an ideological vision on users. For comparison, see this entry in the jargon file:
http://www.catb.org/jargon/html/B/bondage-and-discipline-language.html
Also, I wouldn't mention "best practices" in the same sentence as "Python" within spitting distance of c.l.p unless you want to risk getting slapped in the face with a wet fish.
I personally like this Fibonacci number calculator, written in Python of course:
ReplyDeletefibonacci = lambda x:map(lambda o:(map(lambda c:map(lambda l:
o.__setslice__(l[0],l[1],l[2]),([o[2]+3,o[2]+4,[o[0]]],[0,3,[o[1],
reduce(lambda x,o:x+o,o[:2]),o[2]+1]])),range(x)),o)[1],[[1,1,0]+
range(x)])[0][3:]
I'm so glad Python forced me to write readable code! Just kidding :) After working in Perl for a summer, it's an absolute lifesaver to be able to count on the consistent readability of Python. Thanks for the article!
I think a lot of the good you see in Python code comes from the the community. People (like Eric, above), know about the evils of unreadable code and when they write it, they usually say that it is bad.
ReplyDelete