I want in, doggone it

Oct 11, 2010 11:18

It's funny. When I started out with Python, I'd really only programmed in TI-83, C, and Mathematica. And Maple, a bit. And HTML, although I don't think of that as a "programming" language. (I guess it is, it just produces static results.)

So, after those punctuation-heavy languages, I was pretty squeamish about Python's preference for leaving all those conditionals and sub-functions and what-have-you hanging out in the open without any braces or brackets or parentheses to corral them. I mean, seriously, expecting the computer to recognize the loop a line of code belongs to just based on the indentation? Using nothings as an integral part of your language structure like that? You can't even get HTML to recognize multiple spaces without beating it over the head with a [pre] tag! (It won't even respect spacing within [code] tags, as seen below. Good grief.)

Another feature of Python is how it handles iterating over a list. Instead of, say, Mathematica, where I'd have to type out something like

For[ i = 1, i <= Length[my_list], i++,
. do stuff with my_list[[i]] ]

Python allows me to type

for thing in my_list
. do stuff with thing

which, quite aside from the lack of punctuation, is MUCH quicker to type than the Mathematica version. Especially if it's a "for" loop where I'm referring to the current list element a lot of times in the code and I've let my variable names get lengthy like I usually do. It took me quite a while to get used to this pattern, and I didn't really realize how much it had come to seem a natural part of programming until just now.

MatLab is definitely more streamlined than C, and it's quick with lists -- it was written specifically to deal with matrices, which are really just lists when you get down to it -- and it uses ":" for ranges rather like Python does (another feature I was wary of at first). But it doesn't have that lovely "in" keyword. And I'd forgotten that just now when I decided to add the chance for some checkpoints in a loop in the program I'm working on. I wanted to do something like

nstep = input('Max # of iterations: ')
chkpts = input('List of checkpoints: ')

[...]

for j = start:nstep
. [...]
. if j is in chkpts
. . fprintf('Hello!\n')
. end
. [...]

and then I realized that that wouldn't do. MatLab doesn't have "in" or "is", silly!

Now, it might have a function to check whether an input value is an element in a particular set, but the MatLab documentation is not exactly the best-organized in the world (that honor would go to Mathematica v5, in my limited experience) and I can't be bothered to scrounge around on the Internet for half an hour when I can just type, in place of the bolded part above,

. for k = 1:length(chkpts)
. . if j == chkpts(k)
. . . fprintf('Hello!\n')
. . end
. end

and be done with it. It's cumbersome, that's all, really. It's even useful, when you want to (for example) look at k+1 or k-1 at the same time you're looking at k.

Anyway, kinda odd how you can get used to something and not even realize it, even to the point of thinking you aren't used to it at all.

(I hope that never happens to me with regards to Windows Vista or 7, though. That way lies sadness.)

computers

Previous post Next post
Up