Giving Python Another Shot
2008-11-11 15:59 - Programming
I've been introduced to and worked with Python in dribs and drabs in the past. I've read plenty about it, or that mentioned it. I'm somewhat (and growing more, by the day) familiar with its advantages. And I'm interested.
Thing is, I'm a web developer. The vast majority (though definitely not all) of things I program are web pages. For a web page, 99% of the input comes from a database or form submission, and 99% of the output goes to a database or HTML page. And it's all just strings. In PHP, the type conversion is handled automatically for you, and there's thousands of times I find that very useful for each time I have even a minor issue with it. In Python, not so. Thus:
$ php -r 'print "5"+4; print "\n";' 9 $ python -c 'print "5"+4' Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'int' objects
This was one of the big things that put me off, when last I tried to get in bed with Python. It truly is the little things. The other is the module system. While I can easily find every function defined in the PHP manual, that's much less true with Python.
I am working on the Project Euler problems, as a tangible thing to "do" as I give learning Python another shot. I'm reading Dive Into Python and Thinking In Python (among others), but I learn by doing much better.
So I got to a question asking to count the first-of-the-month days that fell on a Sunday in the 1900s. I decided to "cheat" by simply construction date objects, and checking their day-of-week property. I managed to google up the Python date reference and write the code in a flash. But when I tried it:
Traceback (most recent call last): File "scratch.py", line 4, ind=date(year, month, 1) NameError: name 'date' is not defined
I'm pretty sure I need to import something, but it's not at all clear what. If I look hard enough, I see that the section of the manual that contains this one is talking about "the datetime module", so from that I managed to figure it out. But this is another annoying step. One of the things that turned me off, the first time. I've hit it again, but I'm doing my best to really give it the old college try, and see where it gets me.