Python 3.0 Print Oddity

English: Python logo Deutsch: Python Logo

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.  

Enhanced by Zemanta


Learning Python Online for (mostly) Free and for Fun

Python logo

Image via Wikipedia

I’ve spent the past few months learning Python independently, but I found so many free and friendly online resources that the task of learning could not have been easier.

At first I was discouraged by the spartan IDLE interactive environment, but luckily Microsft released their Python Tools for Visual Studio for either Cpython or IronPython! Having been spoiled by the niceties of the VS IDE, this much needed Python support was the whole package for me: adding debugging, intellisense, interactive environment, project creation support.

Starting from a clean slate free of any Pythonic knowledge, I began month one going over the chapters on the now-defunct site and doing all practice problems on, which cover some language basics.  I supplemented these with 25 problems from, which is of course language agnostic.  

Since doesn’t exist anymore, I’d say the next best equivalent is (still incomplete for the advanced topics at this time) and LearnPythonTheHardWay.  

In the next month, I did all practice problems on which gives some practice in a bit more depth to Python language features.  I also found that doing all of the problems on make for great practice in any language, but I have thus far used them exclusively to practice Python.

I’ve also read the book Data Structures and Algorithms with Python, which takes advantage of Python to create a most practical and beginner friendly introduction to this subject area.  I found these examples to be very educational, except for the very last chapter on trees.  It seemed as if the author got a bit sloppy towards the end with the sample code and didn’t quite finish the last chapter for complexity discussion.  Although this book is not free, it’s highly recommended to programming beginners looking to get more tools under their belt.

Related links

Enhanced by Zemanta

Greatest hits

  • 1,046 hits