I have started to rewrite some of my existing code in python in an earnest effort to master the language (I have been putting it off for a long while now). Anyway, I was working on a parser for a # commented file, this is how I stripped the comments from each line in C:


while(fgets(line, sizeof(line)-1, fp)) {
  char *hash; //ptr to hash mark denoting a comment
  if(line[0] == '#' || line[0] == '\n')
    continue;
  if((hash = strchr(line,'#')) != NULL)
  line[hash - line] = '\0'; //line + hash = memory location of hash
  printf("%s", line);
}

Now that I am beginning to get a handle on python, I am really starting to appreciate its elegance. This line of code accomplishes the same task:

line.strip()[:line.find("#")].strip()

I have not compared the performance of the above line to my c code , but I would assume using all the nested objects would incur a performance hit. I may test this later, but I am pretty sure my c code would be much faster.