Image via Wikipedia
So I tried out the python doc’s code sample for HTMLParser in the shell, but modified it a bit for Python 3.0 and also my own purposes. It seems like if you just re-run the class in IDLE, the behavior will update but old print statements will persist. So strange! I wonder why?
from html.parser import HTMLParserclass MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): if tag == 'p': print ("Encountered a P tag:", tag) print ("Attrib:", attrs) def handle_endtag(self, tag): if tag == 'p': print ("Encountered an end P tag:", tag) def handle_data(self, data): print ("Encountered some data:", data)parser = MyHTMLParser()parser.feed('<html><head><title>Test</title></head>' '<body><h1><p>Parse me!</p></h1></body></html>')
Looks like the correct behaviour, but why does the original print output get displayed?
Encountered some data: Test
Encountered a start tag: p
Encountered some data: Parse me!
Encountered an end tag: p
This must be some residue of the previous Python version, although the docs suggest I should be able to do everything as before except wrap print parameters with the parentheses.
Old: print "The answer is", 2*2New: print("The answer is", 2*2)
I guess in Python 3+, only the str.format is safe to use for all occassions — at the moment.